You are on page 1of 2

GIT CHEAT SHEET

presented by Tower - the best Git client for Mac and Windows

CREATE BRANCHES & TAGS MERGE & REBASE


Clone an existing repository List all existing branches Merge <branch> into your current HEAD
$ git clone ssh://user@domain.com/repo.git $ git branch -av $ git merge <branch>

Create a new local repository Switch HEAD branch Rebase your current HEAD onto <branch>
$ git init $ git checkout <branch> Don‘t rebase published commits!
$ git rebase <branch>
Create a new branch based on your current
LOCAL CHANGES HEAD) Abort a rebase
$ git branch <new-branch> $ git rebase --abort
Changed files in your working directory
$ git status Create a new tracking branch based on a Continue a rebase after resolving conflicts
remote branch $ git rebase --continue
Changes to tracked files 
$ git checkout --track <remote/branch>
$ git diff Use your configured merge tool to solve
Delete a local branch conflicts
Add all current changes to the next commit 
$ git branch -d <branch> $ git mergetool
$ git add .
Mark the current commit with a tag Use your editor to manually solve conflicts
Add some changes in <file> to the next commit and (after resolving) mark file as resolved
$ git tag <tag-name>
$ git add -p <file>
$ git add <resolved-file>
Commit all local changes in tracked files $ git rm <resolved-file>
UPDATE & PUBLISH
$ git commit -a
List all currently configured remotes
Commit previously staged changes UNDO
$ git remote -v
$ git commit
Show information about a remote Discard all local changes in your working
Change the last commit directory
$ git remote show <remote>
Don‘t amend published commits! $ git reset --hard HEAD
Add new remote repository, named <remote> 
$ git commit --amend Discard local changes in a specific file 
$ git remote add <shortname> <url>
$ git checkout HEAD <file>
Download all changes from <remote>, but
COMMIT HISTORY Revert a commit (by producing a new commit
don‘t integrate into HEAD
Show all commits, starting with newest with contrary changes)
$ git fetch <remote>
$ git revert <commit>
$ git log
Download changes and directly merge/integ-
Show changes over time for a specific file rate into HEAD Reset your HEAD pointer to a previous commit
…and discard all changes since then
$ git log -p <file> $ git pull <remote> <branch>
$ git reset --hard <commit>
Who changed what and when in <file> Publish local changes on a remote
…and preserve all changes as unstaged
$ git blame <file> $ git push <remote> <branch>
changes
Delete a branch on the remote $ git reset <commit>
$ git branch -dr <remote/branch>
…and preserve uncommitted local changes
Publish your tags $ git reset --keep <commit>
$ git push --tags

30-day free trial available at


www.git-tower.com The best Git Client for Mac & Windows
VERSION CONTROL
BEST PRACTICES

COMMIT RELATED CHANGES TEST CODE BEFORE YOU COMMIT USE BRANCHES
A commit should be a wrapper for related Resist the temptation to commit some- Branching is one of Git‘s most powerful
changes. For example, fixing two different thing that you «think» is completed. Test it features - and this is not by accident: quick
bugs should produce two separate commits.
thoroughly to make sure it really is completed and easy branching was a central requirement
Small commits make it easier for other de-
velopers to understand the changes and roll and has no side effects (as far as one can tell). from day one. Branches are the perfect tool
them back if something went wrong. While committing half-baked things in your to help you avoid mixing up different lines
With tools like the staging area and the abi- local repository only requires you to forgive of development. You should use branches
lity to stage only parts of a file, Git makes it yourself, having your code tested is even more extensively in your development workflows:
easy to create very granular commits. important when it comes to pushing/sharing for new features, bug fixes, ideas…
your code with others.

COMMIT OFTEN
AGREE ON A WORKFLOW
Committing often keeps your commits small WRITE GOOD COMMIT MESSAGES
and, again, helps you commit only related Git lets you pick from a lot of different work-
changes. Moreover, it allows you to share your Begin your message with a short summary of flows: long-running branches, topic bran-
code more frequently with others. That way your changes (up to 50 characters as a gui- ches, merge or rebase, git-flow… Which one
it‘s easier for everyone to integrate changes deline). Separate it from the following body you choose depends on a couple of factors:
regularly and avoid having merge conflicts. by including a blank line. The body of your your project, your overall development and
Having few large commits and sharing them message should provide detailed answers to deployment workflows and (maybe most
rarely, in contrast, makes it hard to solve the following questions: importantly) on your and your teammates‘
conflicts. › What was the motivation for the change? personal preferences. However you choose to
work, just make sure to agree on a common
› How does it differ from the previous
workflow that everyone follows.
implementation?
Use the imperative, present tense («change», HELP & DOCUMENTATION
not «changed» or «changes») to be consistent
DON‘T COMMIT HALF-DONE WORK with generated messages from commands Get help on the command line
like git merge.. $ git help <command>
You should only commit code when it‘s com-
pleted. This doesn‘t mean you have
to complete a whole, large feature before VERSION CONTROL IS NOT
A BACKUP SYSTEM FREE ONLINE RESOURCES
committing. Quite the contrary: split the
feature‘s implementation into logical chunks http://www.git-tower.com/learn
and remember to commit early and often. Having your files backed up on a remote http://rogerdudler.github.io/git-guide/
But don‘t commit just to have something in server is a nice side effect of having a http://www.git-scm.org/
the repository before leaving the office at the version control system. But you should not
end of the day. If you‘re tempted to commit use your VCS like it was a backup system.
just because you need a clean working copy When doing version control, you should pay
(to check out a branch, pull in changes, etc.) attention to committing semantically (see
consider using Git‘s «Stash» feature instead. «related chan-ges») - you shouldn‘t just
cram in files.

30-day free trial available at


www.git-tower.com The best Git Client for Mac & Windows

You might also like