davideisinger.com

My personal website
Log | Files | Refs | README

index.md (2641B)


      1 ---
      2 title: "Three Magical Git Aliases"
      3 date: 2012-04-25T00:00:00+00:00
      4 draft: false
      5 canonical_url: https://www.viget.com/articles/three-magical-git-aliases/
      6 ---
      7 
      8 Git is an enormously powerful tool, but certainly not the most
      9 beginner-friendly. The basic commands are straightforward enough, but
     10 until you wrap your head around its internal model, it's easy to wind up
     11 in a jumble of merge commits or worse. Here are three aliases I use as
     12 part of my daily workflow that help me avoid many of the common
     13 pitfalls.
     14 
     15 ## GPP (`git pull --rebase && git push`)
     16 
     17 **I can't push without pulling, and I can't pull without rebasing.** I'm
     18 not sure this is still a point of debate, but if so, let me make my side
     19 known: I hate hate *hate* merge commits. And of course, what does Git
     20 tell you after an unsuccessful push?
     21 
     22     Merge the remote changes (e.g. 'git pull') before pushing again.
     23 
     24 This will create a merge commit, regardless of whether there are any
     25 conflicts between your changes and the remote. There are ways to prevent
     26 these merge commits [at the configuration
     27 level](https://viget.com/extend/only-you-can-prevent-git-merge-commits),
     28 but they aren't foolproof. This alias is.
     29 
     30 ## GMF (`git merge --ff-only`)
     31 
     32 **I can't create merge commits.** Similar to the last, this alias
     33 prevents me from ever creating merge commits. I do my work in a topic
     34 branch, and when the time comes to merge it back to the mainline
     35 development branch, I check that branch out and pull down the latest
     36 changes. At this point, if I were to type `git merge [branchname]`, I'd
     37 create a merge commit.
     38 
     39 Using this alias, though, the merge fails and I receive a warning that
     40 this is not a [fast-forward
     41 merge](https://365git.tumblr.com/post/504140728/fast-forward-merge). I
     42 then check out my topic branch, rebase master, and then run the merge
     43 successfully.
     44 
     45 ## GAP (`git add --patch`)
     46 
     47 **I can't commit a code change without looking at it first.** Running
     48 this command rather than `git add .` or using a commit flag lets me view
     49 individual changes and decide whether or not I want to stage them. This
     50 forces me to give everything I'm committing a final check and ensure
     51 there isn't any undesirable code. It also allows me to break a set of
     52 changes up into multiple commits, even if those changes are in the same
     53 file.
     54 
     55 What `git add --patch` doesn't do is stage new files, so you'll have to
     56 add those by hand once you're done patching.
     57 
     58 ------------------------------------------------------------------------
     59 
     60 Hope you find one or more of these aliases helpful. These *and more!*
     61 available in my
     62 [dotfiles](https://github.com/dce/dotfiles/blob/master/.aliases#L7).