Paul's Tutorials - logoGit Operations Reference


Programming Home

One of the issues with the number of different GUIs and interfaces for git is that it can be difficult to communicate particular functions. Git is, at its core, a commandline program. Most everywhere online, you will see solutions and instructions for git in commandline form, because that is the one standardized interface across all platforms.

As such, it is important to know what all of the commands do, even if you do not plan on using the commandline interface extensively.

This is not a definitive list of git commands, and neither does it explain all of the functions of every command. If you have questions about a command or some of its functions, use the man command with the name of the command you want to look up as an argument (for example, man git-commit). If it's a git command, prefix git- to the command name. Or, if you don't want to use the commandline, you can just Google with the man command as your search query instead.

bash notes

When you're using bash (Note: this is NOT cmd.exe!), there are a couple of things you need to keep in mind.

File paths

File paths in bash separate directories with forward slashes (/). Appending a slash to an argument will tell bash that you're referring specifically for a directory.

Referring to files and folders in the present working directory is done just by typing the filename (no prefixes). If you want to use an absolute path (from the root of the system), lead with a slash /. On Windows, the first folder under / is the drive letter, e.g. /c/ for C:.

You can move through multiple directories at once by separating each folder with a slash, for example:

/home/pt/Documents

Character escaping

There are a number of characters that the shell uses to denote various functions that take precedence over literals such as filenames. For the most part, filenames don't include these characters, but there is one that you will encounter often: the space.

Spaces are used in bash to delimit arguments. So, if you enter a filename that has a space in it the way you normally would, bash will separate everything before the space and everything after the space into two different arguments — preventing your command from functioning properly. So, if you want to pass a filename that has a space or other reserved character, you need to escape it with a backslash:

My\ File.txt

There are a number of other characters that are also reserved, such as parentheses, or even the backslash itself (if you have a backslash in a filename, you double it up: \\). All of these need to be escaped with a backslash. See here for a more complete list.

Optionally, if it's just spaces you're worried about, you can enclose the filename or literal in double quotes. Then you don't have to escape the spaces.

"My File.txt"

Shell autocompletion

If you're typing something like a long filename (or some git references like branches), you can actually have the shell complete the text for you, provided that you've given the shell enough information to do autocompletion.

Start typing the first few characters of the file/folder/branch name, and then hit Tab. If you've given it enough information, it will complete the rest. However, if there are multiple references that have a similar name, bash will only autocomplete as much that is similar between the files. You will have to type the characters to differentiate it from the rest.

Shell expressions

It is possible to make the shell do a search through the contents of the current directory, or, if used with grep, the contents of a file or standard input. This is usually done with a wildcard, *:

pauls-*

This will match any string that begins with the text pauls-, such as pauls-tutorials.

Piping output

It is possible to take the output of a command and use it as the input for another. This is known as piping, and is accomplished with the pipe |.

ls | less

This redirects the output of the ls command and writes it to less's standard input. For those who know C++, this is like taking the cout of one program and giving it as cin for another.

Standard bash Commands

When you're using the commandline, you will also want to know some of the basic features of the shell you're using.

These commands only work in bash! Do not try to use them in cmd!

For these commands, if you want to look them up, omit the git- prefix, since they're not actually git commands.

cd
Change directory — This changes the current working directory, which is essentially equivalent to opening a folder in a file manager. When you want to change to a directory that's in your current working directory, then just pass the name of the directory as the first argument and hit Enter. You can also jump through several directories at once by separating each directory with a /. You can also change directories using absolute paths (from the root of the drive), by prefixing a / to the beginning of the file path. Under most Windows implementations, the root of the C: drive is /c/ (and /d/ for D:, /e/ for E:, etc.).
Notecd does not have man page. Its only function is to switch directories as described here.
pwd
Print working directory — Prints a path to the present working directory.
ls
List directory contents — If no argument is passed, it lists the contents of the current working directory. Or, if a path to a directory is passed, then list the contents of that directory. Some useful switches:
-l
Show file details. Output is in the following format (with vertical lines denoting fields — ls will put spaces here):
permissions | number of items (always 1 if it's a file) | user | group | size | last modified (3 fields) | filename
-h
Show file sizes in a human-readable format (e.g. 1k instead of 1024). To be used with -l
-t
Order by file modification time.
-a
Show all files, including hidden files.
grep
Filter inputgrep searches through standard input for a given expression and writes the result to standard output. Or, if a second argument is passed, search through the file passed as the second argument for the search query given in the first.
This is useful for searching through large/multiple files or logs for something specific. Usually used with a pipe |:
$ ls -l | grep myfile
-rw-r--r-- 1 pt users 0 Aug 30 13:20 myfile.txt
mv
Move or rename files/folders — Move files or folders specified by the first through second-to-last arguments to the path specified as the last argument. If the last argument does not exist, or is a filename, mv will instead rename the file or folder as the second argument.
cp
Copy files/folders — Copy the files or folders specified as the first through second-to-last arguments to the path specified in the last argument. If you need to copy directories, make sure to include the -r switch before the rest of your arguments!
rm
Delete file or directory — This deletes any files or folders passed as arguments. If you want to delete directories, make sure you pass the -r switch before your arguments!
Noterm is a permanent deletion. It does not send files to the recycle bin.
mkdir
Create directory — This command creates one or more directories named according to its arguments. Make sure you escape or enclose the arguments in quotes if it contains spaces.
touch
Create file or update modification time — If the file does not exist, create it as a blank file. Or, if the file does exist, update the file metadata so that it appears to have been modified at the time the command was run.
cat
Read contents of file to standard output — This prints all of the contents of a file directly to the terminal. This will require that you scroll through the terminal history if the file is large. If you don't want to print directly to the terminal, use less.
less
Show contents of file — This displays the contents of the file passed as an argument in a full-window display mode, with arrow-key controls. Or, if no file is passed, then less reads from standard input.
You can search for a string by striking /, typing your search key, and hitting Enter. Use n to jump to the next result.
Strike q to quit.
Note — You cannot edit the file with less. Use vi instead.
vi
Unix commandline text editor — This will open the file in a full-window text editor. Git will usually open vi for commit messages. Some useful vi commands (these must be run from outside of edit mode):
vi cheat sheet
i
Enter insert mode and edit the file. To leave insert mode, strike Esc.
:w
Save the file.
:x
Save the file and quit.
:q
Quit.
:q!
Quit, discarding changes
Notevi is usually provided in Windows bash implementations.
man
Display manual page — This command opens up the manual of the command passed as an argument in less.

Git Commands

These are many of git's own commands, which follow the root git command. If you want to look up a particular command's manual, you need to add the git- prefix to the command.

To see an exhaustive list of git commands, see man git.

init
Initialize git repostiory — Initializes a blank repository in the current working directory.
clone
Clone git repository — Clones a git repository specified by the URI passed as an argument into a folder named with the repository's name in the current working directory. A git repository is initialized in this directory with a remote origin set up to refer to the cloned URI.
status
Check repository status — Displays a status message for the repository.
add
Add files to the staging area — Adds the specified files to the staging area.
-u
Add all tracked files with changes to the staging area.
-A
Add all files to the staging area, even if they are not tracked.
-f
Force git to add a normally ignored file to the staging area.
rm
Delete files from repository — Like rm, git rm will delete a file, but also removes them from the index. Normally, using normal rm will also make git see the change and remove it from the index anyway, but git rm adds some new features for dealing specifically with the git repository:
--cached
Only delete the file from the index. Leave the file in the working directory.
The same consideration is in place for deleting directories — use the -r switch.
commit
Make a commit — Opens a text editor (usually vi) to write a commit message, then commits everything in the staging area with the commit message. Aborts committing if no commit message is present.
-a
Same as git add -u followed by a git commit.
-m <message>
Make a commit with message message. Make sure that message is enclosed in quotes.
log
Show commit log for the currently checked-out branch — Opens up the commit log for the currently checked out branch in less.
push
Push commits — Without any arguments, this pushes everything that has changed (branches, new commits, tags, etc.) to the default remote repository.
Standard arguments are:
git push <remote> <refspec>
where remote is the name of the remote repository and refspec is, under most circumstances, the name of the branch you wish to push to.
pull
Pull commits — Without any arguments, this fetches all changes from the default remote repository and merges them into the working tree.
Standard arguments are:
git pull <remote> <refspec>
where remote is the name of the remote repository and refspec is, under most circumstances, the name of the branch you wish to pull from.
fetch
Fetch remote changes and update remote-tracking branches — This grabs any changes on the remote repository and reflects them in remote-tracking branches.
Standard arguments are:
git fetch <remote> <refspec>
where remote is the name of the remote repository and refspec is, under most circumstances, the name of the branch you wish to fetch.
branch
Manage branches — Without any arguments, this displays all of the local branches and the branch that you're currently on. With one argument, it creates a new branch named according to the argument from the current HEAD. With two arguments, it creates a new branch named according to the first argument from the reference (branch, commit, or tag) given as the second argument. When creating a branch, this does not automatically switch to the new branch.
-d <branch>
Delete branch.
-m [branch] <new-name>
If present, rename branch to new-name. If a branch name is not present (only one argument), then rename the current branch to new-name
checkout
Change working directory to match the specified HEAD — When used with a branch or a tag, this makes the working directory reflect the HEAD of the specified branch or tag. Any commits made when checked out as a branch will be made to the checked-out branch. When used with a commit, this detaches HEAD and makes the working tree reflect the specified commit. Any commits made while in this state will be thrown out upon checkout of another reference unless a new branch is made from the detached HEAD.
-b <new-branch> [source]
Create a branch new-branch and check it out. If source is present, then create the branch from there instead of the current HEAD.
merge
Merge changes from specified reference into the working tree — Usually used when merging branches, but implicit in a pull. This takes all of the changes from the branch specified as an argument that aren't present in the currently checked-out branch and merges them into the working tree. May result in a merge conflict. Be sure to commit the result if not a fast-forward.
--theirs
If merge conflicts occur, resolve them using their copy.
--mine
If merge conflicts occur, resolve them using my copy.
mergetool
Launch a merge tool to resolve conflicts — Git will launch the set merge tool to resolve conflicts in text files. If the files are binary, or the conflict is due to a deletion, mergetool will ask which version you wish to use.
Merge tools include:
  • emerge
  • gvimdiff
  • kdiff3
  • meld
  • vimdiff
  • tortoisemerge
Note — With newer versions of TortoiseGit, using git mergetool will NOT launch the merge tool due to a program rename. Use the graphical method instead.
tag
Create and manage tags — Creates, modifies, and deletes tags. Requires at least an argument to name the tag, but can be used like git branch to create the tag from a reference other than HEAD. When creating a tag, you should always use the -a option and leave a tag message (much like a commit message).
diff
Show a diff — With no arguments, this command shows a diff of the working tree against HEAD in less. Alternatively, you can view a diff between arbitrary commits by passing the commit hashes (or a HEAD reference) as arguments.
You can make a patch file for a diff to apply on another repository by writing the diff to a file:
git diff > diff.patch
apply
Apply a patch — Applies the patch passed as an argument to the working tree. You may need to pass some switches to get git to apply the patch correctly (git will tell you which one you need to use if this is the case). See man git-apply for more details.
config
Configure git — Configure various git settings. The setting is usually denoted with feature.setting as the first argument, and the setting's contents as the second argument. Some useful settings:
user.name
Your name, which will appear on commits. When setting this, make sure your name is enclosed in quotes. This must be configured to make a commit.
user.email
Your email, which will appear on commits. When setting this, make sure your email is enclosed in quotes. This must be configured to make a commit.
credential.helper
If set to cache, this will make git store your password for 15 minutes after you enter it, in case you are going to be making mutiple pushes in a short time. This only works for command-line git.
Using the --global switch while creating or editing a setting will set the setting as default for all repositories.