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:
- Stash your changes
- Pull in the changes from origin
- 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.
curl https://github.com/driftyco/ionic/pull/999.patch | git am
: now we have this PR locally- Test it out
- If you don't like it, just
git reset --hard origin/master
. - 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 - use
git commit --amend
if you want to edit the commit message some more - Now you want to push, but your git repo is out of date.
git pull --rebase origin master
- Push up the code!
git push origin master
the last list is the important one