Page 1 of 1 |
|
Posted: Thu, 9th Dec 2021 21:09 Post subject: Git question |
|
 |
Hey Git cracks, I have a short question:
Let's say I have multiple commits into master:
A, B, C, D, HEAD
now I want to revert D, C and B so that I'm "back" at A, but I also want to keep the changes from B, C and D in a seperate branch (one branch, containing all changes in this order), so that I can merge them at a later time?
|
|
Back to top |
|
 |
|
Posted: Thu, 9th Dec 2021 22:26 Post subject: |
|
 |
โขโข
Last edited by paxsali on Thu, 4th Jul 2024 23:03; edited 5 times in total
|
|
Back to top |
|
 |
LeoNatan
Banned
Posts: 73193
Location: Ramat Gan, Israel ๐ฎ๐ฑ
|
Posted: Thu, 9th Dec 2021 22:39 Post subject: |
|
 |
Create a branch from the point you are in (D).
Then go back to your previous branch and move HEAD to A.
|
|
Back to top |
|
 |
|
Posted: Thu, 9th Dec 2021 22:43 Post subject: |
|
 |
โขโข
Last edited by paxsali on Thu, 4th Jul 2024 23:03; edited 2 times in total
|
|
Back to top |
|
 |
LeoNatan
Banned
Posts: 73193
Location: Ramat Gan, Israel ๐ฎ๐ฑ
|
Posted: Thu, 9th Dec 2021 22:51 Post subject: |
|
 |
Code: | โ Desktop mkdir git-test
โ Desktop cd git-test
โ git-test git init
Initialized empty Git repository in /Users/lnatan/Desktop/git-test/.git/
โ git-test git:(master) touch a.txt && git add -A && git commit -m "A"
[master (root-commit) caef74f] A
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 a.txt
โ git-test git:(master) touch b.txt && git add -A && git commit -m "B"
[master 9f497b4] B
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 b.txt
โ git-test git:(master) touch c.txt && git add -A && git commit -m "C"
[master 27ec611] C
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 c.txt
โ git-test git:(master) touch d.txt && git add -A && git commit -m "D"
[master 83f0d91] D
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 d.txt
โ git-test git:(master) git --no-pager log
commit 83f0d91ddcbe253e8c0d4e01ddf87d30e368b33b (HEAD -> master)
Author: Leo Natan <leonatan@invisionapp.com>
Date: Thu Dec 9 23:46:02 2021 +0200
D
commit 27ec611f7c7d47a24866e2525d7a4d7bfc81bb0a
Author: Leo Natan <leonatan@invisionapp.com>
Date: Thu Dec 9 23:45:50 2021 +0200
C
commit 9f497b40c175e493b724470ec5b0f4fb79fa8459
Author: Leo Natan <leonatan@invisionapp.com>
Date: Thu Dec 9 23:45:41 2021 +0200
B
commit caef74f9cb2b01de5437e16b387b2c266b4f39be
Author: Leo Natan <leonatan@invisionapp.com>
Date: Thu Dec 9 23:45:20 2021 +0200
A
โ git-test git:(master) git checkout -b bcd
Switched to a new branch 'bcd'
โ git-test git:(bcd) git --no-pager log
commit 83f0d91ddcbe253e8c0d4e01ddf87d30e368b33b (HEAD -> bcd, master)
Author: Leo Natan <leonatan@invisionapp.com>
Date: Thu Dec 9 23:46:02 2021 +0200
D
commit 27ec611f7c7d47a24866e2525d7a4d7bfc81bb0a
Author: Leo Natan <leonatan@invisionapp.com>
Date: Thu Dec 9 23:45:50 2021 +0200
C
commit 9f497b40c175e493b724470ec5b0f4fb79fa8459
Author: Leo Natan <leonatan@invisionapp.com>
Date: Thu Dec 9 23:45:41 2021 +0200
B
commit caef74f9cb2b01de5437e16b387b2c266b4f39be
Author: Leo Natan <leonatan@invisionapp.com>
Date: Thu Dec 9 23:45:20 2021 +0200
A
โ git-test git:(bcd) git checkout master
Switched to branch 'master'
โ git-test git:(master) git reset --hard caef74f9cb2b01de5437e16b387b2c266b4f39be #The "A" commit
HEAD is now at caef74f A
โ git-test git:(master) git --no-pager log
commit caef74f9cb2b01de5437e16b387b2c266b4f39be (HEAD -> master)
Author: Leo Natan <leonatan@invisionapp.com>
Date: Thu Dec 9 23:45:20 2021 +0200
A
โ git-test git:(master) git merge bcd
Updating caef74f..83f0d91
Fast-forward
b.txt | 0
c.txt | 0
d.txt | 0
3 files changed, 0 insertions(+), 0 deletions(-)
create mode 100644 b.txt
create mode 100644 c.txt
create mode 100644 d.txt
โ git-test git:(master) git --no-pager log
commit 83f0d91ddcbe253e8c0d4e01ddf87d30e368b33b (HEAD -> master, bcd)
Author: Leo Natan <leonatan@invisionapp.com>
Date: Thu Dec 9 23:46:02 2021 +0200
D
commit 27ec611f7c7d47a24866e2525d7a4d7bfc81bb0a
Author: Leo Natan <leonatan@invisionapp.com>
Date: Thu Dec 9 23:45:50 2021 +0200
C
commit 9f497b40c175e493b724470ec5b0f4fb79fa8459
Author: Leo Natan <leonatan@invisionapp.com>
Date: Thu Dec 9 23:45:41 2021 +0200
B
commit caef74f9cb2b01de5437e16b387b2c266b4f39be
Author: Leo Natan <leonatan@invisionapp.com>
Date: Thu Dec 9 23:45:20 2021 +0200
A |
It just works.
|
|
Back to top |
|
 |
LeoNatan
Banned
Posts: 73193
Location: Ramat Gan, Israel ๐ฎ๐ฑ
|
Posted: Thu, 9th Dec 2021 22:53 Post subject: |
|
 |
If you have a remote, you will need to push --hard after resetting the original branch.
|
|
Back to top |
|
 |
|
Posted: Thu, 9th Dec 2021 22:58 Post subject: |
|
 |
โขโข
Last edited by paxsali on Thu, 4th Jul 2024 23:03; edited 2 times in total
|
|
Back to top |
|
 |
|
Posted: Thu, 9th Dec 2021 23:17 Post subject: |
|
 |
LeoNatan wrote: | Create a branch from the point you are in (D).
Then go back to your previous branch and move HEAD to A. |
/thread
|
|
Back to top |
|
 |
|
Posted: Thu, 9th Dec 2021 23:35 Post subject: |
|
 |
โขโข
Last edited by paxsali on Thu, 4th Jul 2024 23:03; edited 2 times in total
|
|
Back to top |
|
 |
|
Posted: Thu, 9th Dec 2021 23:46 Post subject: |
|
 |
Depending on the project and number of contributors he will want to make considerations to his branching strategy. For a personal project, sure you can be loose and commit to master if you like, but if you have multiple dev's you should adopt a good branching strategy that caters for collaboration and supports a good release process.
In my opinion git-flow is still the golden standard and caters to any scenario during the development lifecycle. Most developers are resistant to adopt it as they feel its 'too hard' and go for simplified strategies that can back you into a corner.
2c but this is a good guide.
https://www.atlassian.com/git/tutorials/comparing-workflows/gitflow-workflow
|
|
Back to top |
|
 |
|
Posted: Fri, 10th Dec 2021 00:36 Post subject: |
|
 |
We do use a process similar to gitflow and only commit production ready code to master, but the guy is new to the project and he just worked on a feature and when he thought it was done it was merged to master..(incl. delete feature branch) and then a bug was found... so he created a new branch from master and merged it again.. and then again. this happened over 3 weeks. No other code was luckily merged into that project (but one guy might've created a new branch based on master inbetween that time). So we want to roll back all 3 commits by our dude, because other teams need to deliver some other app first and we'll keep his stuff in a feature branch until they are ready to commit back into the codebase... it's a mess. His stuff shouldn't have been merged in the first place :/
|
|
Back to top |
|
 |
LeoNatan
Banned
Posts: 73193
Location: Ramat Gan, Israel ๐ฎ๐ฑ
|
Posted: Fri, 10th Dec 2021 01:17 Post subject: |
|
 |
Follow what did it. Thatโs how you do it.
|
|
Back to top |
|
 |
|
Posted: Fri, 10th Dec 2021 01:25 Post subject: |
|
 |
Aha interesting, a good practice worth adopting is through your version control platform (pretty much all platforms support it) is to not allow push by default for dev and master, set a default minimum approval for merge requests to your dev and master branches.
This will provide some advantages:
- No-one can just merge a feature into dev or cut release to master without someone else in the team reviewing and approving the change.
- Gives the chance for everyone to review and provide feedback / constructive criticism, this also really helps less experienced dev's learn through others code, comments, and learn how to review others peoples work properly.
- It's not overly constraining, you could also limit merge request approvals to more senior members of the team, but I often think its better to let everyone in the team have an equal say/responsibility.
- Depending on team size you increase min. approvals required, for 4-5 devs 1 approval is enough, for 6-7+ its good to add two reviewers / approvals before something can be merged.
Your colleague probably feels like shit but not totally their fault as it sounds like you have a review mechanism in place, don't be too hard on them.
|
|
Back to top |
|
 |
|
Posted: Fri, 10th Dec 2021 01:49 Post subject: |
|
 |
Nah we are really not hard on him, we just want to make sure that we properly roll back and keep the changes. Actually if the other teams had listened to our architectural decisions from two months ago, we wouldn't have to roll back shit, because their changes would've been ready :/
|
|
Back to top |
|
 |
|
Posted: Fri, 10th Dec 2021 09:37 Post subject: |
|
 |
PumpAction wrote: | Hey Git cracks, I have a short question:
Let's say I have multiple commits into master:
A, B, C, D, HEAD
now I want to revert D, C and B so that I'm "back" at A, but I also want to keep the changes from B, C and D in a seperate branch (one branch, containing all changes in this order), so that I can merge them at a later time? |
The solutions here are way too complicated. Create a new branch from master. Switch back to master. Run: git reset HEAD~3. Then run: git push --force.
PC: Yes. Console: No.
|
|
Back to top |
|
 |
|
|
Back to top |
|
 |
|
Posted: Sun, 22nd Jan 2023 23:09 Post subject: |
|
 |
|
|
Back to top |
|
 |
Page 1 of 1 |
All times are GMT + 1 Hour |