September 3, 2024

Git: Move all non-pushed commits to a new branch

Note: this the first part of the wiki which shows how to only push the last commit to a remote: https://yctct.com/git-push-specific-commits-only-remote

# Note: Any changes not committed will be lost.
$ git branch newbranch      # Create a new branch, saving the desired commits
$ git checkout main         # checkout main, this is the place you want to go back
$ git reset --hard HEAD~3   # Move main back by 3 commits (Make sure you know how many commits you need to go back)
$ git checkout newbranch    # Go to the new branch that still has the desired commits

or instead of step 3:

$ git reset --hard a1b2c3d4

were the last bit is the hash of the commit you want to reset to.


personal computing command-line interface (cli) gnu linux trisquel shell literacy office applications wiki git

No affiliate links, no analytics, no tracking, no cookies. This work © 2016-2024 by yctct is licensed under CC BY-ND 4.0 .   about me   contact me   all entries & tags   FAQ   GPG public key

GPG fingerprint: 2E0F FB60 7FEF 11D0 FB45 4DDC E979 E52A 7036 7A88