My favorite git commands

This collection mainly serves me as a reminder what cool git commands exist.

  • Delete a branch using git push origin :mybranch
  • Create a tag using git tag -a mytag
  • Delete a tag using git push --delete origin mytag

Stashing

  • Staged and unstaged uncommitted changes using git stash push -m 'mydescription'
  • Re-apply *keeping the stash* using git stash [apply/pop] stash@{xy}
  • Stash untracked changes aswell by adding -u
  • Stash ignored and untracked by adding -a
  • List all stashes using git stash list
  • Create branch out of stash git stash branch mybranch stash@{xy}
  • Delete stash using git stash drop stash@{xy}

Merging strategies

The base command is git checkout main && git merge feature [options]

  • Fast forward (--ff-only): feature branch has new commits, which the main branch lacks. All feature commits are appended to main.
  • Recursive (-s recursive): Default. Is complex and sophisticated.
  • Ours (-s ours): Resolves conlflicts by always picking main > feature
  • Octopus (-s octopus): Aborts when conflicts occur.
  • Resolve (-s resolve): Only resolves trivial conflicts.
  • Subtree (-s subtree): Use this in case feature is a filesystem subtree of main.

Rewriting history – the unscrewer

Interactive rebases let you resolve and fix every mistake you ever made.

Rebases work by pretending you started committing ontop of the current main branch, instead of what really happened, you started working on an older version of the main branch, it got updated in the meantime and now the changes of it need to be present in your feature branch.

To perform a rebase do git rebase main inside the feature branch. Or git fetch && git rebase origin/main in case you don’t want to first update the main branch to the newer state.

Now for rewriting history, you can always start a rebase with ‘yourself’, or rather a past commit of the current branch. To do so do git rebase -i HEAD~42. This starts a rebase with the base being the commit 42 commits ago. You get a vim window and can pick, drop, squash, reword, and most importantly fixup a commit. The latter deleting the fixup commit and amending into the commit before. This is very helpful for small hotfixes and keeping the pull requests small in terms of commits.

Leave a Reply

Your email address will not be published. Required fields are marked *