Want to change my host to an older commit, how can I do this?

I want to go back to the previous commit, and then post this code, and then go back to the last commit.

i.e. so my wizard points to an earlier version of the commit so that I can scare this version, and then I want to go back to the last commit, from which I was originally.

How can i do this?

+72
git
Dec 05 '10 at 16:04
source share
7 answers

If you want to do this and revert the wizard to the previous commit:

git checkout master~1 # Checkout previous commit on master git checkout -b new_master # Create branch for new master git branch -D master # Delete old master git branch -mv new_master master # Make new_master master 

As an alternative:

 git reset --hard master~1 # Reset current branch to one commit ago on master 
+88
Oct 17 '12 at 18:15
source share

Your question is not clear. I think you are asking:

 git push -f origin $old_commit_id :master 

What will it be? It pushes $old_commit_id commit on origin as the new header of the origin s master branch.

If this is what you wanted, you do not need to touch the local master branch at all.

+47
Dec 06 2018-10-12T00:
source share

use git reset --hard <old commit number>

he will reset HEAD for this old commit.

In addition, you need to use git push -f origin to modify the remote repo.

+25
Jun 10 '17 at 11:38 on
source share

You can just git checkout <commit-id> do everything you need, then git checkout master to return to the new code.

If you really need to change the old code to let it go, you should probably:

 git checkout -b my_release <commit-id> ... prepare code for release ... ... release code ... git checkout master git merge my_release 

Also, I cannot recommend git flow enough. It makes it all pretty simple.

+8
Dec 05 '10 at 16:33
source share

Assuming a commit graph as follows:

 | (A) ---------> (B) ----------> (C) | ^ | (master) 

You want to check master first and create a branch that indicates where master is located:

 git checkout master git branch pointer master 

It should now look like this:

 | (A) ---------> (B) ----------> (C) | ^ | (HEAD, master, pointer) 

Now that you are already on master , we will tell the master branch to move back one commit:

 git reset master~1 

Now master should be moved back one space, but the pointer branch is still in the last commit:

 | (A) ---------> (B) ----------> (C) | ^ ^ | (HEAD, master) (pointer) 

At this point, you can push master to the remote or anywhere, and then quickly move it back to the pointer branch. You can kill the pointer branch at this point:

 git push origin master git merge --ff-only pointer git branch -D pointer 

The final:

 | (A) ---------> (B) ----------> (C) | ^ ^ | [ origin/master ] (HEAD, master) 
+8
Aug 29 '14 at 18:43
source share

To upgrade to the previous version:

git checkout <version hash>

do your job here and copy it using

git commit --amend

To return to the master :

git checkout master

+2
Dec 05 '10 at 16:11
source share

If you want to avoid forced pushing, here's how to get the repo back into an older commit and save all the intermediate work:

 git checkout 307a5cd # check out the commit that you want to reset to git checkout -b fixy # create a branch named fixy to do the work git merge -s ours master # merge master history without changing any files git checkout master # switch back to master git merge fixy # and merge in the fixed branch git push # done, no need to force push! 

Done! Replace 307a5cd with whatever commit you want in your repo.

(I know that the first two lines can be combined, but I think it makes it less clear what is going on)

Here it is graphically:

 c1 -- c2 -- c3 -- c4 -- c2' -- c5 ... \ / '------------' 

You effectively remove c3 and c4 and return your project back to c2. However, c3 and c4 are still available in your project history if you ever want to see them again.

0
Jun 21 '19 at 21:56 on
source share



All Articles