Skip to content

Instantly share code, notes, and snippets.

@lym
Forked from mhartington/git-conventions.md
Created December 10, 2021 17:40
Show Gist options
  • Save lym/3117cfb11af1151be0a05a9300fb60de to your computer and use it in GitHub Desktop.
Save lym/3117cfb11af1151be0a05a9300fb60de to your computer and use it in GitHub Desktop.
Git Conventions

You've been working locally for awhile. You're ready to push some changes, but someone else pushed something! You have to pull in their changes before you can push yours.

git pull origin master forces you to create a merge commit, which is annoying.

Instead, do git pull --rebase origin master. This will effectively:

  1. Stash your changes
  2. Pull in the changes from origin
  3. Apply your changes on top

No merge commit!

Merging in Pull Requests

Someone has a pull request, say at this url: ionic-team/ionic-framework#411 There also exists a 'patch file' representing the commit(s) in their pull request. You can attain it just by adding .patch to the end of their url: https://github.com/driftyco/ionic/pull/411.patch

What if we could just take this patch and apply their commits onto our local version of the repository? We can using git am! We'll just download the patch file and pipe it into git am, which will apply the pull requests' commits on top of our local repository.

curl https://github.com/driftyco/ionic/pull/411.patch | git am git log - now see, you have two new commits on top of your local version

Before you commit, you can use git commit --amend to make sure the name matches our git commit message conventions.

Squashing Commits

Use interactive git rebasing to squash many commits into one.

git rebase -i HEAD~10 opens the last ten commits for interactive rebasing.

In the screen that comes up, follow the instructions to squash all of the many commits for a pull request into one. Then save and quit, and follow the instructions to rename that commit to something good like feat(pencil): add red color, or just use git commit --amend.

Example workflow for merging a Pull Request

Say you open the pull requests page, and see pull request #999 that you want to try out.

  1. curl https://github.com/driftyco/ionic/pull/999.patch | git am: now we have this PR locally
  2. Test it out
  3. If you don't like it, just git reset --hard origin/master.
  4. If you like it, first squash it into one commit: git rebase -i HEAD~6 (imagining there are 6 commits in this PR) and follow instructions to squash
  5. use git commit --amend if you want to edit the commit message some more
  6. Now you want to push, but your git repo is out of date. git pull --rebase origin master
  7. Push up the code! git push origin master

the last list is the important one

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