Git quick reference

Configure your global git settings
Running these commands will setup your global git settings. You should obviously use your own contact details. Should you wish to change your details, you can edit the '~/.gitconfig' file for global settings, or edit '.git/config' to change settings on a particular repo.

See the documentation for many more configuration options.

Display current branch in bash
If you work with branches, and you should, this setting helps you keep track of which branch you are in at a given time. If you are in a git working directory, it shows the current branch as part of the prompt:

[user@host directory-name (master)]$

To enable this, you can take advantage of the  function, provided by   in the git package. Add this line to :

export PS1='[\u@\h \W$(declare -F __git_ps1 &>/dev/null && __git_ps1 " (%s)")]\$ '

If you do not have the bash-completion package installed, you must manually source the git completion script prior to using. To do this, add " " to.

To activate bash configuration changes, run.

In addition to displaying the current branch, this will show when you are in the middle of a merge or rebase.

You might also want to display when there are changes in your work tree or the git index:

[user@host directory-name (master*)]$ [user@host directory-name (master+)]$ [user@host directory-name (master%)]$


 * On the first line, a tracked file was modified
 * On the second line, a tracked file was modified and staged (with )
 * On the third line, you have untracked files in your tree

Of course, those can combine themselves...

To do so, simply add these lines in your, right before the line modifying your prompt:

export GIT_PS1_SHOWDIRTYSTATE=true export GIT_PS1_SHOWUNTRACKEDFILES=true

See the comments at the beginning of  for more details.

Initialize a new repo
Once you've created a repo, you'll find a '.git' folder inside it. What you essentially have at this point is a bare repo -- a repository with the git configs, but no actual files contained in the repository. Now let's create a file and tell git that we want it to be part of our repo. From the repo directory (not the .git directory), type:

We can then commit the changes by typing:

Another way of committing all the changes (without having to specify all the files that have changed) is to type:

Either way, it will bring up whichever editor you have defined in $EDITOR and allow you to write a commit log message explaining the changes you've made. A commit log message usually consists of:


 * a one-line summary of your changes
 * a blank line
 * one or more additional lines with more detail. These lines are optional.

You can always check the status of your current repo by typing

Switch to a branch that was pushed remotely
Use:

to determine the name of the upstream branch you want to work on. Then use this to switch to it:

This creates a local branch named  and tells git that it came from. Using  will show you whether your local branch is ahead, behind, or otherwise different than the upstream branch.

If you want to use a different name for your local branch, you can use:

Remove a remote branch
Assuming you had a branch named blah on the remote server, you could remove it by typing: