How can I change which commit master points to in git?
- stash your uncommitted:
git stash
- create a new branch:
git branch new_branch
- reset master to origin/master:
git reset --hard origin/master
- checkout the new branch again:
git checkout new_branch
- unstash your changes:
git stash pop
stash/unstash is not necessary if your working tree is clean. just make sure there are no changes in your working tree, because those will be removed when you reset --hard
another possibility (faster, and without the need to stash and reset):
- checkout a new branch:
git checkout -b new_branch master
- create a 'new' master branch and point it to origin/master's commit:
git branch -f master origin/master
$ git checkout master
$ git reset --hard <commit-id-for-master-to-sit-at>
for example try this
$ mkdir example; cd example
$ git init
$ vi testFile.txt
(now add "test commit 1" to line 1 of file)
$ git add *
$ git commit
(add message "(+) 1st commit" to git commit)
$ vi testFile.txt
(now add "test commit 2" to line 1 of file)
$ git add *
$ git commit
(add message "(+) 2nd commit" to git commit)
$ vi testFile.txt
(now add "test commit 3" to line 1 of file)
$ git add *
$ git commit
(add message "(+) 3rd commit" to git commit)
$ git tag final_head
$ git reset --hard HEAD~1
this example shows moving the master to a different commit. Note here that the tag allows us to save the old master, in case :)
Go to .git/refs/heads/master which has the hash of master and change that to whatever you want. I use gitg to quickly find the hash of master and afterwards to verify that the move was successful.