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:
Clone a current repo (into a folder with the same name as repo):
Clone a repo into a specific folder name:
Clone a repo into the current directory (should be an empty directory):
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):
Create a remote repo named origin
pointing at your GitHub repo (using SSH URL instead of HTTP URL):
Show the names of the remote repositories you’ve set up:
Show the names and URLs of the remote repositories:
Remove a remote repository:
Change the URL of the git repo:
Show the files changed:
Show changes to files compared to the last commit:
Show changes in a single file compared to the last commit:
Show changes between two different commits:
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:
Show who changed each line of a file and when:
Go back to the last commit (will not delete new unstaged files):
Undo/revert the last commit AND create a new commit:
Undo/revert a specific commit AND create a new commit:
Stage all files (new, modified, and deleted):
Stage new and modified files (not deleted):
Stage modified and deleted files (not new):
Remove a file and untrack it:
Untrack a file only:
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:
Stage files (modified and deleted, not new) and commit:
Take the uncommitted work (modified tracked files and staged changes) and save it:
Show list of stashes:
Reapply the latest stashed contents:
Reapply a specific stash (e.g., stash@{2}
):
Drop a specific stash:
Push your changes to the origin:
Push a branch to the origin:
Tag a version (e.g., v1.0
):
Useful for GitHub releases.
Get the latest changes from origin
(don’t merge):
Get the latest changes from origin
AND merge:
Get a remote branch from origin
into a local branch (naming the branch and switching to it):
Show all branches (local):
Show all branches (local and remote):
Create a branch from HEAD
:
Create a new branch and switch to it:
Switch to an already created branch:
Push a branch up to the origin
(GitHub):
Get a remote branch from origin
into a local branch (naming the branch and switching to it):
Delete a branch locally and remotely:
Merge a specific branch into the main branch:
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:
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:
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!