1

I would like to split my work into two branches, so I would like to assign a new branch name to the current tip and move my current branch name back 3 commits. The 3 new commits have not yet been pushed.

I currently have this:

branch1                     F - G - H  
                           /
origin/branch1    C - D - E
                 /
master      A - B 

And I would like this:

branch2                              F - G - H  
                                    /
branch1, origin/branch1    C - D - E
                          /
master               A - B 

I already created branch2:

git checkout -b cleanup_exchanges

Could you please advise me on the next step, to take branch1 back a few commits without moving branch2?

PiRK
  • 893
  • 3
  • 9
  • 24

2 Answers2

3

Branch is nothing but a pointer to a commit, so you can freely move the branch1 pointer to any other commit.

This should work

git checkout branch1
git reset --hard origin/branch1

or to go some N commits back:

git checkout branch1
git reset --hard branch1~3
Alexey S. Larionov
  • 6,555
  • 1
  • 18
  • 37
1

From this source: https://opensource.com/article/18/6/git-reset-revert-rebase-commands

Let's start with the Git command reset. Practically, you can think of it as a "rollback"—it points your local environment back to a previous commit. By "local environment," we mean your local repository, staging area, and working directory.

$ git log --oneline
b764644 File with three lines
7c709f0 File with two lines
9ef9173 File with one line

The Reset Command:

$ git reset 9ef9173 (using an absolute commit SHA1 value 9ef9173)

or

$ git reset current~2 (using a relative value -2 before the "current" tag)

Then you can check the logs again

$ git log --oneline
9ef9173 File with one line

In your case you have the origin/branch1 as a reference and you can do this:

git checkout branch1
git reset --hard origin/branch1

In conclusion, you have different ways to go back in time in a branch, even using a non-conventional way as:

git checkout -b 9ef9173 (using an absolute commit SHA1 value 9ef9173)

This will create a new branch an then

git checkout branch1
git merge 9ef9173
Yoandry Collazo
  • 1,122
  • 9
  • 16