The Git Logo
Image from git-scm.com.
I have had the opportunity to explain Git and the basics of its operation on more than one occasion. It's something that I enjoy doing, especially because I believe it is an indispensable skill for collaboration in industry and academia. I have always distributed a small "cheat sheet" with the most-used commands, and I am posting it here (for no reason other than I can find it quickly in the future). I will include the text in this post, but I will also provide the
.txt file that I use. (Yes, I wrote it in a txt file. I like the way it looks, and when I print it out using Notepad++, the wrapped text is indented nicely.
Helpful Git Commands by Corey Pennycuff ================================= === STARTING A GIT REPOSITORY === ================================= git init Initializes an empty git repository in the current directory. git clone URL Initializes a git repository and prepopulates it with the files and history of the repo found at URL. Sets up a remote called 'origin' that points to the repo at URL. Checks out the branch 'master'. ============================= === WORKING WITH BRANCHES === ============================= git branch List all available branches. The branch 'master' is created by default. git branch BRANCHNAME Creates a branch called 'BRANCHNAME' that branches from the current codebase at this point. The branch will have the same commit history as the current branch. NOTE: THE BRANCH IS ONLY CREATED, IT WAS NOT SWITCHED TO. YOU WILL STILL BE ON THE ORIGINAL BRANCH. git checkout BRANCHNAME Switches to the branch called 'BRANCHNAME'. git checkout -b BRANCHNAME Combination of 'git branch' and 'git checkout'. Creates the branch 'BRANCHNAME' at this point and switches to that branch. git branch -d BRANCHNAME === OR === git branch -D BRANCHNAME Deletes the branch locally. '-d' only deletes the branch if it has been merged (in other words, makes sure that you will not accidentally lose any of your work). '-D' deletes the branch regardless. ============================ === WORKING WITH COMMITS === ============================ NOTE: 'FILENAME' MAY BE AN ACTUAL FILE NAME, A DIRECTORY, OR A WILDCARD PATTERN (i.e., *.java) git status Shows the status of your repository, if any files have been changed, created, or deleted. git diff Shows any differences in the tracked files, line by line, as referenced against the staging area. (Can be used to create patch files.) git add FILENAME Adds the file 'FILENAME' to the staging area in it's current state. git rm FILENAME Tell the staging area that the file 'FILENAME' will no longer be tracked by the system. git commit -m "MESSAGE HERE" Creates a commit that packages up all the changes to all the files in the staging area, and logs the changes with the commit message given. NOTE: EVEN IF A FILE IS BEING TRACKED BY THE SYSTEM AND HAS BEEN CHANGED, THE CHANGE WILL NOT BE ADDED TO THE COMMIT UNLESS IT WAS FIRST ADDED TO THE STAGING AREA USING 'git add FILENAME'. git commit -am "MESSAGE HERE" SHORTCUT. The "-a" part tells git to take any changes in the currently tracked files and "a"dd them to the staging area, then perform the commit as normal. NOTE: THIS COMMAND WILL NOT ADD NEW, UNTRACKED FILES, AND IT WILL NOT REMOVE DELETED FILES. THIS MUST BE DONE FIRST, WITH THE APPROPRIATE add/rm COMMAND. ======================================== === WORKING WITH A REMOTE REPOSITORY === ======================================== git remote Show a list of all remotes. If you initiated your repository with a 'git clone' command, then you will already have a remote named 'origin' that points to the URL that you cloned from. git remote add REMOTENAME REMOTEURL Add a remote named REMOTENAME, at the url REMOTEURL. The REMOTEURL may use the https:// or git:// protocol. git remote -v Show a list of all remotes, as well as their locations and what permissions you have at that repository. git fetch REMOTENAME Pulls the latest changes to the remote REMOTENAME and stores them to your local machine's repo history. NOTE: THIS DOES NOT CHANGE ANY FILES THAT YOU ARE CURRENTLY WORKING ON, IT ONLY MAKES THE remote's NEWEST CHANGES AVAILABLE TO YOU ON YOUR LOCAL SYSTEM. git merge REMOTENAME/BRANCHNAME Looks at the version of REMOTENAME that you have locally (that you received by running the 'git fetch' command), and merges in any changes of it's branch BRANCHNAME into the files currently on your system. This does not stage the changes, however, nor does it add them to a commit. This must be done by using the appropriate 'git add' command. git pull REMOTENAME/BRANCHNAME Does a fetch followed by a merge. The main difference is, however, that not all of REMOTENAME is fetched, only the branch BRANCHNAME. Personal option is to use fetch and merge individually. git checkout REMOTENAME/BRANCHNAME Checks out the version of REMOTENAME/BRANCHNAME that you have locally (that you received by running the 'git fetch' command). git push REMOTENAME BRANCHNAME Pushes all new commits in BRANCHNAME to the remote REMOTENAME. Please do not run 'git push' without specifying REMOTENAME and BRANCHNAME. It is sloppy and, although you *can* set up defaults and tell git what to do when you run this command, it is better to run the full version of this command so that you know with certainty what is happening. ============================== === OTHER HELPFUL COMMANDS === ============================== NOTE: In the following commands, <IDENTIFIER> can be one of several things. Examples are: REMOTENAME/BRANCHNAME The local version of REMOTENAME/BRANCHNAME HEAD (this is literally the word 'HEAD' , in all caps) The last commit of the current branch. This DOES NOT include any changes that have been added to the staging area, which is different from the simple 'git diff' command shown above. A hash tag from the commit log. This references a specific commit that has already been made. The hash tag is quite long, but you do not have to use the entire thing. Just use enough of the tag that git can tell which one you are referring to. This may be as few as 3 or 4, but most people use 6 or 7. git diff Described above. NOTE: diff shows the difference of the current code against the staging area. git diff <SOME SORT OF IDENTIFIER> Shows the difference between the current code and <SOME SORT OF IDENTIFIER> git diff <IDENTIFIER>..<IDENTIFIER> Shows the difference between two points as referenced by the identifiers listed above. Example: git diff origin/master..w98u28u09 git log Shows a history of commit messages and related info. Exit by pressing 'q'. git show <HASH> Shows the additions and deletions that were involved in a specific commit as referenced by <HASH>. git help <COMMAND> git <COMMAND> --help Shows the help documentation for <COMMAND>. Exit by pressing 'q'. git log --graph --decorate --pretty=oneline --abbrev-commit Just try it. :) ======================================================================= === OTHER THINGS TO LOOK UP WHEN YOU FEEL MORE COMFORTABLE WITH GIT === ======================================================================= tag, reset, stash and pop, grep, blame, rebase (know what it is, but please avoid), bisect Do the free tutorial at http://www.codeschool.com