git stash -k
https://stackoverflow.com/a/14506564/1244574
- Pop our 1st commit
git stash pop
- Make a temp commit, will undo at step 4.
git add . && git commit -am 'WIP'
- Pop our 2nd commit
git stash pop
- Undo our temp commit
git reset --soft HEAD^
https://stackoverflow.com/a/32318688/1244574
git rebase -p --onto SHA^ SHA
A git fixup is prefered, as it is easier for reviews to see if you addressed their comemnts. So this is "OK" if a PR review hasn't been started yet https://stackoverflow.com/a/1186549/1244574
git rebase --interactive 'SHA^'
- Replace "pick" with "edit" and save and close the file
- Note, you won't see any staged local changes. Might be able to do a
git rest --soft
to see these
- Note, you won't see any staged local changes. Might be able to do a
- Make your changes
git commit --amend
git rebase --continue
git push --force-with-lease
Make your changes, then do git add file.name
as normal but instead of a normal commit do git commit --fixup orginal_commit_hash
.
git push
this like normal and request another PR review.
After your PR is approved run the following to git commands to clean up the history.
git rebase -i --autosquash BRANCH_MERGING_INTO
git push --force-with-lease
This way the fixup commits will be cleaned up and it will look like you got it right the first time!
This covers using the git rebase --onto
command. Summary of these otpions:
git rebase --onto <newparent> <oldparent>
git rebase --onto <newparent> <oldparent> <until>
This site explains git rebase --onto
well: https://womanonrails.com/git-rebase-onto
- "newparent" - Find this by looking at the branch you branched from, copy the newest commit hash.
- "oldparent" - Find this by looking at your branch, copy the newest commit that doesn't belong on this branch.
git checkout YOUR_BRANCH
- Run
git rebase --onto newparent oldparent
git checkout your_branch
git rebase --onto branch_you_branched_from origin/branch_you_branched_from@{####}
- Replace
####
with the number of force pushes your branch is behind (when you branch or since you last rebased)
origin/
and @{#}
are important here as they are the previous known git reflog on github before it was rebased.
- If this number is samller than it should be then git rebase will show conflicts that are unrelated to your commits. If larger, it will simply error without doing anything.
- Check
git log
or github and count the number of commits you know are yours- VERY important to get the count correct, if your short you can lose commits.
- If you don't see any extra commits that shouldn't be there you can do a normal
git rebase
git rebase --onto branch_you_branched_from your_branch~#### your_branch
- NOTE: replace "####" with your number of commits
- Use
git log
to ensure all your commits you want are there. - Also use
git log
to ensure your commits are just above the branch you expect. - Run
git push --force-with-lease
to make it live!
Here's a few aliases that I have in my ~/.gitconfig [alias] section:
cleanup
: delete local branches that have been merged to master - I end up with so many branches in my local repos, so this helps with thatpop
: undo the last commit, but leave the files in the same state as they were after the commit, useful if you want to re-organize commits, or you committed something you didn't want to