How to move a branch backwards in git?
Solution 1:
Use the reset subcommand:
git checkout A
git reset --hard B
git push --force github
As a sidenote, you should be careful when using git reset
while a branch has been pushed elsewhere already. This may cause trouble to those who have already checked out your changes.
Solution 2:
If there are no commits on branch A
, then the git reset --hard B
solution given by Bram Schoenmakers will work.
However if there are commits are branch A
which must be preserved, then the following should do the trick:
- Make a backup copy of your repo (just in case)
git checkout A
git rebase -i --onto B SHA1-A^
...where SHA1-A^
is the commit id of the parent of your branch A
See the git rebase
man page for details.
NOTE: This will rewrite history (as rebase always does). Special consideration should be made if your A
branch was ever pushed to a public repo.