Skip to content

Instantly share code, notes, and snippets.

@coopy
Last active March 26, 2019 22:50
Show Gist options
  • Save coopy/377ab74e850c782c8404 to your computer and use it in GitHub Desktop.
Save coopy/377ab74e850c782c8404 to your computer and use it in GitHub Desktop.
Good git practices

Good Git Practices

1. Commit often!

  • Each commit in a final pull request should contain one logical change, and tests + checks should run.
  • While you're working on a branch, you should commit often, but you don't have to follow these rules. Instead, once your work is complete, you can use interactive rebase or reset to create new, better organized commits.

2. Squashing commits: No rebase required

Say you have 5 commits that you'd like to squash into one or two. Simply do this:

# double-check your commit history
$ git log --oneline -10
# => [list of commits]

# convert last 5 commits to local changes
$ git reset HEAD~5

# Option a) Commit everything in one commit
$ git commit -a

# Option b) Add specific changes, commit as you go
# See next section "Know what you commit"

# If you had already pushed your branch, you have to
# force-push now. THIS CHANGES HISTORY. DO NOT DO THIS
# ON SHARED BRANCHES LIKE `master`! Shared feature
# branches can be fine, as long as you tell whomever you're
# working with that they'll have to do a `git pull --rebase`.

$ git push --force

##3. Know what you commit

  • git add -p

  • y to add hunk

  • n to ignore

  • s to split up hunk

  • When you're done committing, git reset --hard to get rid of cruft

    $ git add -p
    $ git commit -m "Make widget work"
    
    $ git add -p
    $ git commit -m "Add test for widget"
    
    $ git add -p
    $ ...
    

4. Don't fear the rebase

  • git rebase --interactive

  • f to squash and use parent commit message

  • s to squash and reword commit message

  • r to edit commit message

  • e to edit commit content AND commit message

    # Print compact list of last 5 commits
    $ git log --oneline -5
    
        a238e22 Remove NoErrorsPlugin
        6170b9c Use --inline option in hot webpack config
        97b717f Upgrade react-hot-loader
        f1cd6f8 Use only-dev-server to not reload on syntax errors
        73b385f v0.0.9
    
    # Detach commits up to `sha` and do interactive stuff with them
    $ git rebase -i 97b717f^
    
        pick 97b717f Upgrade react-hot-loader
        pick 6170b9c Use --inline option in hot webpack config
        pick a238e22 Remove NoErrorsPlugin
    
        # Rebase f1cd6f8..a238e22 onto f1cd6f8
        #
        # Commands:
        #  p, pick = use commit
        #  r, reword = use commit, but edit the commit message
        #  e, edit = use commit, but stop for amending
        #  s, squash = use commit, but meld into previous commit
        #  f, fixup = like "squash", but discard this commit's log message
        #  x, exec = run command (the rest of the line) using shell
        #
        # These lines can be re-ordered; they are executed from top to bottom.
        #
        # If you remove a line here THAT COMMIT WILL BE LOST.
        #
        # However, if you remove everything, the rebase will be aborted.
    

5. Write high quality commit messages

    Add `node_modules` to `.gitignore`

    The summary is less than 50 characters long. The description
    follows the summary after 2 newlines, and can contain more
    detail.

<img width="999" alt="screen shot 2016-01-14 at 11 06 54 am" src="https://cloud.githubusercontent.com/assets/794843/12334470/074a6228-baaf-11e5-953f-1f7a00669fa8.png">

6. Set up aliases in ~/.gitconfig

    [alias]
      ap = add -p
      amend = commit --amend
      br = branch
      ci = commit
      cm = commit
      co = checkout
      lo = log --oneline
      st = status

7. Use git for everything!

    $ git init
    $ git commit -a -m "Add all the initial things"
    # Make changes. Change change change.
    # Wait 1 week. What was I working on?
    $ git diff # AAAAH.
@coopy
Copy link
Author

coopy commented Oct 14, 2016

Thanks @paulathevalley!

Gist updated.

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