Git Cheatsheet

git

git config --global user.name "Your Full Name Or Screen Name"
git config --global user.email "Your Email Address"
git config --global color.ui auto // Enables helpful colorization of 
  // command line output

Typical flow:

1. Create a new branch on the remote server
2. git pull
3. git checkout branchName
4. modify our files as desired
5. git status . // List out the files that we modified
6. git diff fileName // Review the changes
7. git add fileName
8. git commit -m "Commit message must contain the defect number"
9. git branch // Check the name of the branch that we were working on
10. git push origin branchName
11. Create a pull request from the branch that we were working to the release / integration branch

git status // Lists all new or modified files to be committed
git add fileName
git reset fileName // unstage a file
git commit -m "Descriptive message"

git add . // stage all modified and new files
git commit -a // automatically stage all tracked, modified files and commit

git rm fileName // Deletes the file from the working directory and stages 
  // the deletion
git rm --cached fileName // Removes the file from version control but 
  // preserves the file locally.  Undo its changes in the staging area
  // but the file itself still exists in version controll
git mv originalFileName newFileName // Changes the file name and 
  // prepares it for commit

git diff // Shows changes that we have done (but have not been committed) to 
  // all files that were changed
git diff fileName // Shows changes that we have done to a specific file
git diff --staged // Shows file differences between staging and the last file 
  // version
git diff --cached // Show diff of staged changes
git diff HEAD // show diff of both staged and unstaged changes together (basically
  // a diff of the file against the last commit)
git diff [first-branch]...[second-branch] // Shows content differences between 
  // two branches
git diff remotes/origin/branch1...remotes/origin/branch2 

git show [commit] // Outputs metadata and content changes of the specified 
  // commit

git branch // Lists all local branches in the current repository
git branch -a // shows all local and remote branches
git branch -r // show only remote branches
git branch branchName // Creates a new branch
git branch -d branchName // Deletes the specified local branch name from the 
  // local repository
git checkout branchName // Switches the current working copy to the 
  // specified local branch name and updates the working copy to the latest from 
  // that branch
git merge branchName // merge changes from the specified local branch name into 
  // the current working copy
git fetch originName // Downloads all history from the specified remote
git merge originName/branchName // Merge the given branch on the remote
  // origin into current local working copy
git merge --abort

git push remoteName remoteBranchName // push commits that had been committed
  // to the local repository to the given remote branch name on the given remote 
  // repository

git pull // Downloads all history from the tracking remote and merge changes
  // that had been pushed to the tracking remote into the current working copy.

git stash // Temporarily stores all modified tracked files to some place
git stash pop // Restores the most recently stashed files
git stash list // Lists all stashed changesets
git stash drop // Discards the most recently stashed changeset

git log // Lists version history for the current branch
git log --follow fileName // Lists version history for a file, including renames

git reset [commit] // Undoes all commits after [commit], preserving changes 
  // locally
git reset --hard [commit] // Discards all history and changes back to the 
  // specified commit

git init projectName
git clone repoURL

mkdir projectName
cd projectName
git init

git clone /path/to/repository
git clone username@host:/path/to/repository
git clone repoURL

git checkout -b feature_x // Creates a new branch named "feature_x" and switch 
  // to it.

git push origin feature_x // Push the branch to the remote server.

git branch -d feature_x // Deletes a branch

git diff sourceBranchName targetBranchName // See differences between two branches.

git tag tagName 1b2e1d63ff // Creates a tag given the specific commit ID.

git log --author=bob // See only the commits from a certain authors.
git log --pretty=oneline // See a very compressed log where each commit is one line.
git log --graph --oneline --decorate --all // See an ASCII art tree of all the branches, 
  // decorated with the names of tags and branches.
git log --name-status // See only which files have changed.

git checkout -- fileName // This is the equivalent of 'svn revert'.  This replaces the changes 
  //  in our working tree with the last content in HEAD.  Changes already added to the 
  // index, as well as new files, will be kept.  If we want to drop all local changes and 
  // commits, fetch the latest history from the server and point our local master branch 
  // at it:
git fetch origin
git reset --hard origin/master

git reset // Undo the last git add
git reset fileName // Undo the last git add for that file
git reset HEAD -- fileName
git reset HEAD fileName
Graphical tools:
gitk
Git Extension

How can we unstage a file?

git reset [file] // unstage a file while retaining the changes in working directo

What does the 'git diff' command do?

git diff // diff of what is changed but not stage
git diff --stage // diff of what is staged but not push
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License