git command cheatsheet
Clone an existing repository.
git clone "user@domain.com/reponame.git"
Create a new local git repository in the directory myproj.
mkdir myproj cd myproj git init
Show changed files in the working directory.
git status
Show difference of tracked files.
git diff
Show the names of the changed files from between the tenth latest commit and the fifth latest
git diff --name-only HEAD~10 HEAD~5
Stash the uncommitted local changes, stack them so they will be ignored during commit or merge. This is usually used when switch between branches.
git stash
Get the local changes that were stashed before back.
git stash pop
Add file for the next commit.
git add filename1 filename2 filename3...
Add all changed file for the next commit.
git add .
Commit all the staged files which we did by git add.
git commit -m "this is a comment for this commit"
Changes the commit message from the last commit.
git commit --ammend -m "This is a new message which will replace the message committed in the last commit"
Shows all commit history for the current working branch.
git log
Shows all the commit history for the specified branchname.
git log branchname
Shows all the commit history for the specified filename.
git log -p filename
Shows who changed what and when in the specified filename.
git blame filename
Checkout out the projectOne branch from the remote and name it projectOne in your local git repository.
git checkout -b projectOne origin/projectOne
Lists all existing branches.
git branch
Change the branch name
git branch -m old-branch-name new-branch-name
Change the current working branch name
git branch -m new-branch-name
Switch the working directory to the master branch.
git checkout master
Switch the working directory to the projectOne branch.
git checkout projectOne
Create a new branch based on your current branch.
git branch "new branch name"
Push the specified branchname to the remote repository.
git push origin branchname
Deletes the specified branchname.
git branch -d branchname
Deletes the specified branchname in the remote repository, which has to be followed by detleting the local repository branch first.
git push origin :branchname
Create a tag based on the current working branch.
git tag "tagname"
Push a tag to the remote repository.
git push origin tagname
Pushes all the tags to the remote repository.
git push origin --tags
Checkout a branch from a tag.
git checkout -b mynewbranch thetagname
Deletes the specified tagname.
git tag -d tagname
Deletes the specified tagname in the remote repository, which has to be followed by deleting the local repository tag first.
git push origin :tagname
Lists remote repository addresses.
git remote -v
Shows information about the specified remote address.
git remote show
Download all changes but not merge them.
git fetch origin
Download all changes and merge them in to the current working branch.
git pull origin
Pushes the committed changed in the current working branch to the specified remote branch.
git push origin branchname
Merges the specified branchname into the current working branch.
git merge branchname
Merges the changes in the specified commit-number into the current working branch.The commit number can be from any branch.
git cherry-pick commit-hashcode
Remotes the specified filename
git rm filename
Undo the last local commit, keep the changes.
git reset --soft HEAD^
Undo the last local commit, throw away the changes.
git reset --hard HEAD^
Revert back to the specified revision#.
git reset --hard revision#
Reset username and get prompt for username and password when pushing
git config --local credential.helper "" git push origin master
Search within Codexpedia
Search the entire web