39
jchw
8y

!rant.
Here's some useful git tricks. Use with care and remember to be careful to only rewrite history when noones looking.

- git rebase: powerful history rewriting. Combine commits, delete commits, reorder commits, etc.

- git reflog: unfuck yourself. Move back to where you were even if where you were was destroyed by rebasing or deleting. Git never deletes commits that you've seen within at least the last 50 HEAD changes, and not at all until a GC happens, so you can save yourself quite often.

- git cherry-pick: steal a commit into another branch. Useful for pulling things out of larger changesets.

- git worktree: checkout a different branch into a different folder using the same git repository.

- git fetch: get latest commits and origin HEADs without impacting local braches.

- git push --force-with-lease: force push without overwriting other's changes

Comments
  • 1
    What exactly is the difference between pull and fetch?
  • 3
    @SirWindfield pull = fetch + merge
  • 1
    These are all very good so I think I'll add my personal favorite powertool to your list (I know you mentioned rebase already but I think this one deserves special mention):

    git rebase -i

    It's like rebase with 290% more magic since you can change how each commit is applied (including changing individual commits!). Oh how I love that command when I have to follow the Gerrit workflow.
  • 0
    I used cherry pick very often when i was on a team that handled production issues. Often times other team members would commit fixes to feature branches that were nowhere near ready, but we needed the fix ASAP.

    So much cleaner than duplicating the work in a new branch.
  • 0
    @capnsoup I probably should've just included the "-i" since that's probably the most useful feature of rebase. (Normally if I rebase a branch onto another I just use the --rebase flag with pull instead of rebase, because it makes sure I don't forget to fetch.)
Add Comment