Git cheat-sheet

As a recent cross-over user of Subversion to Git, I occasionally need a refresher of some command-line git operations … and I find myself searching the web.  So, I created this list in a single, easy to locate post.  Here is a list of Git operations I find most useful.  Listed in order of a typical workflow.  Hence more interesting (and more easily forgotten) operations are listed at the end.

Initial setup
sudo apt-get install git
Initial configuration
git config --global user.name "Iam me"
git config --global user.email me@here.com
git config --global core.editor nano
git config --global credential.helper "cache --timeout=900"
Configure ignored files (using .gitignore file)
nano .gitignore
*.o
nohup.out
doc/*.txt
.vscode
.idea
Help
git help <command>
Initialize a repository
git init
OR
git clone git://your.git.url.goes.here
View status
git status
git diff
Add/commit files
git add <filename>
git commit
OR
git commit -am "commit message goes here"     (add all tracked files and commit them in a single command)
Remove files
git rm <filename
Move files
git mv <filename> <newfilename>
Updating a commit (before push)
git commit -am 'initial commit'
git add <somefile>
git commit --amend
View commits
git status
Reverting a modified file (not yet staged)
git checkout -- <file
Reverting a staged file (e.g. via git add)
git reset HEAD <file
Reverting a commit
git revert <commit-id>
git push
Reverting all local changes
git reset --hard origin/master
Create a new branch
git checkout -b <name_of_your_new_branch>
git push origin <name_of_your_new_branch
Save/restore work temporarily
git stash
***change branches or whatever
git stash pop
Delete a remote branch
git push origin --delete <branchName>
git fetch -p    "to refresh local repro"
Delete a local branch
git branch -d <branch_name>
git fetch -p    #"to refresh local repro"
Tag a commit (for release or tracking)
git tag -a <tagname> <commitid>
git push origin <tagname>
Checkout at a tag
git checkout <tagname>
Merge a branch back into master branch
git checkout master
git pull
git checkout <branch>
git merge master
# if you have conflicts ... edit the conflicts and then run "git add <files_edited>" "git commit"
git push

Leave a Reply