-
Notifications
You must be signed in to change notification settings - Fork 15
Git workflow
The following command will create a new branch locally and switch to it. If you have any local changes already, they will be kept. So you can either do this command before or after you start doing changes.
git checkout -b my-branch
The first time you push a new branch, you need to set the remote as upstream:
git push -u origin my-branch
After that, new commits can be pushed by simply:
git push
To switch to another branch:
git checkout my-other-branch
After you've merged a branch, you can delete it:
git branch -d my-merged-branch
If you're working in a branch, and there are changes in master
, you can sync your branch with master. You don't have to do this for every change, but it's a good idea to at least do it before merging your branch to master
, to make sure that all the tests still pass. Don't forget to pull
first, to get the latest master
:
git checkout master
git pull
git checkout my-branch
git merge master
git push
To merge your branch into master, either use GitHub to create a pull request and merge it, or merge it locally:
git checkout master
git pull # fetch latest master
git merge my-branch
git push
git branch -d my-branch # optional; delete local branch
Discard local uncommitted changes and return to a clean working directory. (Stashed changes can be restored at a later point if needed.)
git stash
Install Git auto-completion. I find it particularly useful for auto-completing branch names.
Manually backup and restore uncommitted changes. This can sometimes be useful to move changes between branches or repositories, or if you're about to do some complicated git operation, and are concerned that you might lose your changes:
git diff > ~/backup.diff
...
patch -p1 < ~/backup.diff