Post

Git Cheat Sheet

Draft updated on Invalid Date
Default avatar

By Chris on Code

Git Cheat Sheet

This tutorial is out of date and no longer maintained.

Introduction

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.

Git Terminology

  • HEAD - current branch
  • main - default branch we develop in
  • origin - default upstream repo (GitHub)
  • remote - repository stored on another computer
  • staging (adding) - adding changed files to index tree to be committed

Here’s a good glossary of definitions.

Starting a Repo init/clone/remote

Create a repo from existing data:

  1. git init

Clone a current repo (into a folder with the same name as repo):

  1. git clone (repo_url)

Clone a repo into a specific folder name:

  1. git clone (repo_url) (folder_name)

Clone a repo into the current directory (should be an empty directory):

  1. 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):

  1. 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):

  1. git remote add origin git@github.com:username/(repo_name).git

Show the names of the remote repositories you’ve set up:

  1. git remote

Show the names and URLs of the remote repositories:

  1. git remote -v

Remove a remote repository:

  1. git remote rm (remote_name)

Change the URL of the git repo:

  1. git remote set-url origin (git_url)

Showing Changes status/diff/log/blame

Show the files changed:

  1. git status

Show changes to files compared to the last commit:

  1. git diff

Show changes in a single file compared to the last commit:

  1. git diff (filename)

Show changes between two different commits:

  1. 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:

  1. git log

Show who changed each line of a file and when:

  1. git blame (filename)

Undoing Changes reset/revert

Go back to the last commit (will not delete new unstaged files):

  1. git reset --hard

Undo/revert the last commit AND create a new commit:

  1. git revert HEAD

Undo/revert a specific commit AND create a new commit:

  1. git revert (commit_id)

Staging Files add/rm

Stage all files (new, modified, and deleted):

  1. git add -A

Stage new and modified files (not deleted):

  1. git add .

Stage modified and deleted files (not new):

  1. git add -u

Remove a file and untrack it:

  1. git rm (filename)

Untrack a file only:

  1. 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.

  • Working Tree - The “tree” that holds all our current files.
  • Index (after adding/staging file) - The “staging” area that holds files that need to be committed.
  • HEAD - Tree that represents the last commit.

Publishing commit/stash/push

Commit the local changes that were staged:

  1. git commit -m "message"

Stage files (modified and deleted, not new) and commit:

  1. git commit -am "message"

Take the uncommitted work (modified tracked files and staged changes) and save it:

  1. git stash

Show list of stashes:

  1. git stash list

Reapply the latest stashed contents:

  1. git stash apply

Reapply a specific stash (e.g., stash@{2}):

  1. git stash apply (stash_id)

Drop a specific stash:

  1. git stash drop (stash_id)

Push your changes to the origin:

  1. git push

Push a branch to the origin:

  1. git push origin (local_branch_name)

Tag a version (e.g., v1.0):

  1. git tag (tag_name)

Useful for GitHub releases.

Updating and Getting Code fetch/pull

Get the latest changes from origin (don’t merge):

  1. git fetch

Get the latest changes from origin AND merge:

  1. git pull

Get a remote branch from origin into a local branch (naming the branch and switching to it):

  1. git checkout -b (new_branch_name) origin/(branch_name)

Branching branch/checkout

Show all branches (local):

  1. git branch

Show all branches (local and remote):

  1. git branch -a

Create a branch from HEAD:

  1. git branch (branch_name)

Create a new branch and switch to it:

  1. git checkout -b (branch_name)

Switch to an already created branch:

  1. git checkout (branch_name)

Push a branch up to the origin (GitHub):

  1. git push origin (branch_name)

Get a remote branch from origin into a local branch (naming the branch and switching to it):

  1. git checkout -b (new_branch_name) origin/(branch_name)

Delete a branch locally and remotely:

  1. git push origin --delete (branch_name)

Integrating Branches merge/rebase

Merge a specific branch into the main branch:

  1. git checkout main
  2. 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:

  1. 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:

  1. git cherry-pick (commit_id)

Conclusion

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.

Learn more about us


About the authors
Default avatar
Chris on Code

author

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
Leave a comment


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!

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel