git Workflows Quick Reference

In learning git, I have found both the power and confusion that comes with this technology. I have created this page as a place to stash quick references to common git commands and workflows for easy reference. I hope that they prove to be useful to you, as they are for me.

Note that if you don’t know git, the best free reference that I have found is Pro Git by Scott Chacon. You can buy the book at amazon, but you can also just go to the git-scm website and read it there, or download it in various e-reader friendly formats for free.

One Time Setup Actions

Set your name globally:
$ git config --global user.name "John Doe"

Set your email address globally:
$ git config --global user.email johndoe@example.com

Set your merge tool (example assumes using opendiff):
$ git config --global merge.tool opendiff

Project Startup / Common Workflows

Initializing a new project with git:
$ git init

Adding new files / staging changed files (by name) to git
$ git add filename

Adding new files / staging changed files (using wildcards) to git:
$ git add *.cpp

Current status of your files and git:
$ git status

Setting up git to ignore files you don’t want in the repository: (First make sure that you are in the root directory of the project. It has the .git folder in it).
$ cat << EOF > .gitignore
> *.o
> *.a
> EOF

(Note that you can and should use your favorite text editor to modify the .gitignore file.)

Commiting staged changes:
$ git commit -m "commit message"

Commiting changes to tracked files without needing to stage them:
$ git commit -a -m "commit message"

Working with Branches

Creating a new branch:
$ git branch hotfix

Switching to the new branch:
$ git checkout hotfix
Switched to branch 'hotfix'

Getting a list of branches (note the ‘*’ indicates current branch):
$ git branch
* hotfix
master

Switching back to the original branch:
$ git checkout master
Switched to branch 'master'

(I realize the above tip is the same as ‘Switching to the new branch’ above, but it is included to jog the memory as to how git works.)

To create and set a branch in one command:
$ git checkout -b b6751
Switched to a new branch "b6751"

To delete a branch from a remote server:
$ git push origin --delete working
remote: bb/acl: scott is allowed. accepted payload.
To https://scott@someserver.com/ballywig/balloon.git
- [deleted] working