Skip to content

Git cheatsheet

Frequently used commands

Creating a repository

git init
Create a local Git repository
git clone <remote_url>
To obtain a remote repository. Get the remote_url from GitHub

Local changes & commits

git status
View the status of your repository
git add <file_path>
Add a file to the staging area
git rm <file_path>
Remove a file from the staging area
git commit -m "<message>"
Commit everything in the staging area. Choose a concise and descriptive message.
git log
Show the commit history. Press q when reaching the end of the log to exit.
git log --oneline
Show the commit history. Uses a single line per commit. Less detail, easier to keep the overview.

Branching

git branch
Check what branch you are currently on
git checkout <branch_name>
Switch to a different branch
git branch <new_branch_name>
Create a new branch from the current branch
git merge <other_branch>
Merge all the changes on other_branch into the current branch
git branch -d <branch_name>
Delete a (local) branch

Syncing with remote

git remote -v
Show information on the rconfigured remote(s)
git fetch
Obtain changes from remote. This does not change your code locally.
git pull
Obtain the changes to the current branch from remote. Merge them with your local branch.
git push
Upload your local changes to the current branch to remote. Merge them with the remote branch.

Undoing changes

git reset --soft <commit_hash>
Remove the commit with commit_hash. Keep the changes made in that commit in the staging area. Discards history for the commit.
git revert <commit_hash>
Create a new commit that undos all changes in commit_hash. Preserves history for the commit.

Workflow example

In this example, we will add a new function that calculates square roots to our imaginary script mathematics.py. A

  1. Start on your local main branch
  2. git pull to ensure you are up-to-date with remote
  3. git branch feature/sqrt
  4. Write the code in mathematics.py
  5. git add mathematics.py to stage the changes
  6. git commit -m "add square root function"
  7. (optional) Obtain feedback from a peer
    1. git push to upload your local branch to remote. You may need to set the upstream (remote) branch. Git will provide you with the command.
    2. Create a Pull Request on GitHub and ask your peer for feedback.
    3. If the PR is approved, continue with step 8.
  8. git checkout main to return to the main branch
  9. git merge feature/sqrt to merge the changes into main
  10. git push to make sure the changes are also on remote
  11. (optional) git branch -d feature/sqrt to clean-up the feature branch

Further reading