my work, life, and ideas

Posts Tagged ‘git’

Git cheat sheet

This is my personal Git cheat sheet. It is really a mish-mosh of other resources across the web. Yep, there are a million of them. This one isn’t special, but it is useful to me. If it works for you too, awesome; if not, move on.

create a remote branch from master

git checkout -b branch_name master
git push origin branch_name

delete a remote branch

git push origin :branch_name

switching between branches on your local

git checkout branch_name

merging and tagging a release branch to master

git checkout master
git merge --no-ff release_branch_name
#fix any potential merge conflicts (there should be any) and commit
git tag -a 2010.xxx
git push --tags

merging a feature branch back into development

git checkout development
git merge --no-ff myfeature_branch
git push origin development
 # (optionally delete the feature branch, git branch -d myfeature_branch)

bug fixing and patching between branches
Depending on where you are working, commit your fix to the appropriate branch
Then use:

git log | less
# Find your revision (it should be one the latest ones)
git diff your_revision_hash > /tmp/my_patch (ex. git diff cfbe9041b > /tmp/my_patch)
git checkout branch_you_want_to_apply patch
git apply /tmp/my_patch
# continue committing and pushing as you would normally

Aside from merging, sometimes you want to just pick one commit from a different branch. To apply the changes in revision rev and commit them to the current branch use:
note: try patching first, if patching isn’t working well then try cherry picking the revision(s)

git cherry-pick revision_hash

merging release branch hot-fix and bug fixes to development branch
note: Patching and cherry picking are preferred over full merging

git checkout development
git merge --no-ff release_name

stash your changes for later

git stash

find out what is in your stash list

git stash list

apply your stash

git stash apply

clear your stash

git stash clear

notes about your .git/config

You’ll need to make changes to this as often as you contribute to the different number of branches being used for your project. Meaning if you have to commit to the development branch, latest release branch(es), and merge into the master branch – these all need to be in your .git/config file. Here is an example. Notice the rebase = true on the development branch and how it is commented out for the release branch “Alkonost”.

[branch "master"]
remote = origin
merge = refs/heads/master
 
[branch "development"]
remote = origin
merge = refs/heads/development
rebase = true
 
[branch "ReleaseBranch"]
remote = origin
merge = refs/heads/ReleaseBranch
rebase = true