Josh Reback
Josh Reback

Reputation: 579

Rollback in Git

Question about doing a rollback in git:

Let's say the git SHA of the commit I want to rollback to is: 12345.

I tried:

git checkout 12345
git checkout -b rollback
git push origin rollback

Then, when I attempt to compare 'rollback' with master, it says there are no differences - why?

UPDATE: I compared master branch and rollback branch using Github and found no differences.

I found that the following works:

git checkout -b current-state-master
git checkout master
git checkout 12345
git push origin -f

I see why the second one works, but I don't know why the first approach did not work.

Upvotes: 0

Views: 1311

Answers (4)

byname1234
byname1234

Reputation: 130

git reset --hard 12345

git push origin master -f 

warning!: The "--hard" parameter will ignore all the local modifies .

Upvotes: 0

Nils_M
Nils_M

Reputation: 1070

This is my take:

git checkout 12345        #This checks out the commit with SHA starting with 12345
                          #You're in detached HEAD state and
                          #you're in no way related to any branch
git checkout -b rollback  #This creates a new branch starting from the current HEAD,
                          #which is 12345.
git push origin rollback  #This updates branch rollback on origin with the 
                          #local branch rollback

If the HEAD of the master branch differs from the commit with SHA 12345 git diff rollback master will generate some output. I don't know what you're doing in your case, but the problem should lie somewhere else.

A bit more succinct: If git diff master 12345 produces output, then git diff master rollback will as well. git diff rollback 12345 will not.

Following this, your second example will work as well as it is only missing the line, which gives the detached HEAD the name rollback.

So my answer would be: Your problem can not originate from the lines of code you've given.

See git checkout and git branch.

Upvotes: 2

khmarbaise
khmarbaise

Reputation: 97399

What about the simplest one:

git revert 12345

or

git revert HEAD~3

or

git revert -n master~5..master~2

Furthermore doing things in a repo via git reset .. would result in a non forwarding history which will produce pain for others.

Upvotes: 0

Will Vousden
Will Vousden

Reputation: 33358

To follow from Tim Biegeleisen's answer, your first snippet can be corrected like so:

git checkout -b rollback
git reset 12345
git push origin rollback

This will leave "rolled back" commits in the index; use reset --hard to avoid this.

Upvotes: 1

Related Questions