Git Cheat Sheet

Posted on Jan 6, 2021

If you are unfamiliar with git, or need a primer check out Git Basics

Managing Remote Repositories (Cloud Originals)

Copy a remote repository

git clone {repository-link}
git clone
  • This command clones a remote repository into a folder on your computer within the directory you are in.
  • cd {repository-name}
    • After cloning the repository you need to move into the repository folder.
    • The folder name will be the last part of the url you cloned.
    • Example: cd cpython

Updating from Remote

git pull
  • This will update your local copy with any changes to that branch that have been pushed to the remote repository.
  • This is mainly used on the main/master branch, or if you are working on a branch collaboratively, or on multiple devices.
  • It is best practice to always git pull before you start working to avoid unnecessary merges

Adding new branch to remote repository

git push --set-upstream origin {your-branch-name}
  • This will create your branch in your remote repository, so it does not just exist on your computer.

Updating the Remote

git push
  • This will push any changes you have committed locally to the corresponding branch in your remote repository.
  • You want to do this frequently, so your changes are backed up and you do not lose them if something happens to your computer.

Managing Branches (copies)

Creating a new copy

git checkout -b {your-branch-name}
git checkout -b fix/formatting
  • This creates a new copy of all the files in your repository with the name you specify.
  • In programming branches will be proceeded with feature/, or fix/ depending on what the update is doing, followed by a brief description of what the change is doing. For example fix/formatting.
  • This copies all the files currently in the repository. This means that if you are on a branch other than the original your new branch will have all the changes in that branch. Generally you will want to be in the main branch when running this command.

Seeing local branches

git branch
  • Displays the names of all the branches you have locally.

Switch branches

git checkout {branch-name}
git checkout main
  • Switches to the specified branch.

Merging branches

git merge {update-from-branch-name}
git merge main
  • This will pull the updates from the specified branch into your current branch.
  • This is useful if there have been updates to the main/master branch and you want to ensure your code works with these changes. git merge main
  • Merges of a change branch into the main/master branch will generally be handled online in your remote repository.

Delete branch

git branch -D {branch-name}
  • This will delete a branch

Delete all branches other than the default branch

Make sure you are on the default branch

git checkout main
git branch | grep -v '^*' | xargs git branch -D

Delete all branches that have been merged

Make sure you are on the default branch

git checkout main
git branch | grep -v '^*' | xargs git branch -d

Managing Commits (Snapshots)

Seeing changed files

git status
  • This command will show you all the files that have been updated in your repository.
  • Files highlighted in red have not been added.
  • Files highlighted in green have been added.

Seeing differences in files

git diff {file-name}
  • This command enables you to see changes within a specific file.
  • It will display the additions in green and deletions in red.

Adding changes

git add *
git add {file-name}
git add mysite/mysite/
  • Once you have made changes to files you need to specify the updated files you want to group together in a commit.
  • Generally you will use git add *, which adds all the changed files, but you can specify individual files by inputting their name, git add mysite/mysite/

Committing to changes

git commit -m "{what-your-change-does}"
  • Before committing changes they only exist locally and their history is not recorded. Committing changes adds them to your version history.
  • This will realize all the changes you have added and associate them with a unique identifier and the commit message you input.

Removing changes

git reset --hard
  • This command deletes all changes that have not been committed.
  • Sometimes you make changes and do not like them and want to start over.

Reverting a commit

git reset HEAD~
  • This will remove the last commit you made. Your changes will still be there, but they will no longer be committed.