This tutorial is out of date and no longer maintained.
A simple Git cheat sheet for the basic commands and working with a git repo, in our case Github.
To start, you can always use git help
to see a basic list of commands.
HEAD
- current branchmain
- default branch we develop inorigin
- default upstream repo (GitHub)Here’s a good glossary of definitions.
Create a repo from existing data:
- git init
Clone a current repo (into a folder with the same name as repo):
- git clone (repo_url)
Clone a repo into a specific folder name:
- git clone (repo_url) (folder_name)
Clone a repo into the current directory (should be an empty directory):
- git clone (repo_url) .
Create a remote repo named origin
pointing at your GitHub repo (after you’ve already created the repo on GitHub) (used if you git init
since the repo you created locally isn’t linked to a remote repo yet):
- git remote add origin https://github.com/username/(repo_name).git
Create a remote repo named origin
pointing at your GitHub repo (using SSH URL instead of HTTP URL):
- git remote add origin git@github.com:username/(repo_name).git
Show the names of the remote repositories you’ve set up:
- git remote
Show the names and URLs of the remote repositories:
- git remote -v
Remove a remote repository:
- git remote rm (remote_name)
Change the URL of the git repo:
- git remote set-url origin (git_url)
Show the files changed:
- git status
Show changes to files compared to the last commit:
- git diff
Show changes in a single file compared to the last commit:
- git diff (filename)
Show changes between two different commits:
- git diff (commit_id)
Commit ID: This can be that giant long SHA-1 hash. You can call it many different ways. I usually just use the first 4 characters of the hash.
Show history of changes:
- git log
Show who changed each line of a file and when:
- git blame (filename)
Go back to the last commit (will not delete new unstaged files):
- git reset --hard
Undo/revert the last commit AND create a new commit:
- git revert HEAD
Undo/revert a specific commit AND create a new commit:
- git revert (commit_id)
Stage all files (new, modified, and deleted):
- git add -A
Stage new and modified files (not deleted):
- git add .
Stage modified and deleted files (not new):
- git add -u
Remove a file and untrack it:
- git rm (filename)
Untrack a file only:
- git rm (filename) --cached
It will still exist. Usually, you will add this file to .gitignore
after rm
Git Workflow Trees: How adding and committing moves files between the different git trees.
Commit the local changes that were staged:
- git commit -m "message"
Stage files (modified and deleted, not new) and commit:
- git commit -am "message"
Take the uncommitted work (modified tracked files and staged changes) and save it:
- git stash
Show list of stashes:
- git stash list
Reapply the latest stashed contents:
- git stash apply
Reapply a specific stash (e.g., stash@{2}
):
- git stash apply (stash_id)
Drop a specific stash:
- git stash drop (stash_id)
Push your changes to the origin:
- git push
Push a branch to the origin:
- git push origin (local_branch_name)
Tag a version (e.g., v1.0
):
- git tag (tag_name)
Useful for GitHub releases.
Get the latest changes from origin
(don’t merge):
- git fetch
Get the latest changes from origin
AND merge:
- git pull
Get a remote branch from origin
into a local branch (naming the branch and switching to it):
- git checkout -b (new_branch_name) origin/(branch_name)
Show all branches (local):
- git branch
Show all branches (local and remote):
- git branch -a
Create a branch from HEAD
:
- git branch (branch_name)
Create a new branch and switch to it:
- git checkout -b (branch_name)
Switch to an already created branch:
- git checkout (branch_name)
Push a branch up to the origin
(GitHub):
- git push origin (branch_name)
Get a remote branch from origin
into a local branch (naming the branch and switching to it):
- git checkout -b (new_branch_name) origin/(branch_name)
Delete a branch locally and remotely:
- git push origin --delete (branch_name)
Merge a specific branch into the main branch:
- git checkout main
- git merge (branch_name)
Merging: Merging will occur FROM the branch you name TO the branch you are currently in.
Take all the changes in one branch and replay them on another:
- git rebase (branch_name)
Usually used in a feature branch. Rebase the main to the feature branch so you are testing your feature on the latest main codebase. Then merge to the main.
Rebasing: Usually switch to a feature branch (git checkout newFeature
). Then rebase (git rebase main
). Then merge back so you have all the changes of main and the feature branch (git checkout main
, and git merge newFeature
).
Merge just one specific commit from another branch to your current branch:
- git cherry-pick (commit_id)
There you go! Hopefully, that covers most of the basic ones and a few more. If you’d like to see any that haven’t been covered here, I’d be happy to add them. Also if you need a further explanation or demonstration, don’t be scared to ask. Happy gitting!
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!