Skip to content

Instantly share code, notes, and snippets.

@schneefisch
Last active December 8, 2022 10:30
Show Gist options
  • Save schneefisch/5a925197a3ef41ae703d35fbd11b3df8 to your computer and use it in GitHub Desktop.
Save schneefisch/5a925197a3ef41ae703d35fbd11b3df8 to your computer and use it in GitHub Desktop.
rebase and move a git commit

move last commit to new branch

this is when the last commit(s) have accidentially been committed to the wrong branch (i.e. master) but HAVE NOT been pushed

git branch newBranch
git reset --hard HEAD~2 # go back 2 commits, you will lose uncommited work
git checkout newBranch

Rebase

NOTE: do the rebase in a separate terminal and resolve conflicts using IntelliJ IDEAS capabilities https://www.atlassian.com/git/tutorials/merging-vs-rebasing/workflow-walkthrough

go to master and update

git co master
git pull origin master

go to feature branch and rebase (current branch on top of master)

git co <feature-branch>
git rebase -i master

push

git push -f origin <feature-branch>

To "squash" some commits, change the corresponding line:

pick fb554f5 This is commit 1
squash 2bd1903 This is commit 2
squash d987ebf This is commit 3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment