Skip to content

Instantly share code, notes, and snippets.

@jkasten2
Last active April 19, 2024 21:37
Show Gist options
  • Save jkasten2/133a176ed3d0257c5de00046977dbcb6 to your computer and use it in GitHub Desktop.
Save jkasten2/133a176ed3d0257c5de00046977dbcb6 to your computer and use it in GitHub Desktop.

Stash only unstagged lines

git stash -k

Merge two stashes

https://stackoverflow.com/a/14506564/1244574

  1. Pop our 1st commit
    • git stash pop
  2. Make a temp commit, will undo at step 4.
    • git add . && git commit -am 'WIP'
  3. Pop our 2nd commit
    • git stash pop
  4. Undo our temp commit
    • git reset --soft HEAD^

Remove a specific commit completely

https://stackoverflow.com/a/32318688/1244574

git rebase -p --onto SHA^ SHA

Edit a specific commit

1. With rebase (Not recommend)

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

  1. git rebase --interactive 'SHA^'
  2. 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
  3. Make your changes
  4. git commit --amend
  5. git rebase --continue
  6. git push --force-with-lease

2. With git fixup (Recommended)

Part1

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.

Part 2

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!

Rebase

Rebase your branch if the branch under yours was rebased

Summary

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

Step 1

Option 1 - By commit hashes

  1. "newparent" - Find this by looking at the branch you branched from, copy the newest commit hash.
  2. "oldparent" - Find this by looking at your branch, copy the newest commit that doesn't belong on this branch.
  3. git checkout YOUR_BRANCH
  4. Run git rebase --onto newparent oldparent

Option 2 - By a previous origin ref (only works if all rebases were done on the same machine)

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.

Option 3 - By number of commits

  1. 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
  2. git rebase --onto branch_you_branched_from your_branch~#### your_branch
    • NOTE: replace "####" with your number of commits

Step 2

  1. Use git log to ensure all your commits you want are there.
  2. Also use git log to ensure your commits are just above the branch you expect.
  3. Run git push --force-with-lease to make it live!
@lilymara-onesignal
Copy link

Here's a few aliases that I have in my ~/.gitconfig [alias] section:

cleanup = "!git branch --merged master | grep  -v '\\*\\|master\\|develop' | xargs -n 1 git branch -d && git remote prune origin"
pop = "reset --soft HEAD~1"

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 that
pop: 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment