Skip to content

Git Cheat Sheet

Michael Shipman edited this page Feb 9, 2024 · 2 revisions

Adding, Committing, and Pushing

  • Check what files have been changed
git status
  • Stage file, directories, or all changes for a commit
git add . //current directory
git add /path/to/file
git add ./directory
git add --all //all changes
  • Commit changes with message
git commit -m "message"
  • Push changes
git push

Ignore Changes to File and Pull from remote

git fetch
git checkout remote/current-branch /path/to/file
git pull

Unstage a file

  • To remove files from staging, but keep your changes:
git reset HEAD <file>
  • To unstage the last three commits:
git reset HEAD^3
  • To unstage changes to a certain file from HEAD:
git reset <filename>

After you unstage the file, to revert the file back to the state it was in before the changes:

git checkout -- <file>

Documentation about upstaging

Branches

  • List all branches (the one you are currently in is marked)
git branch -a
  • Switch branches
git checkout branch-name

Submodules

Many of the repositories that you might be working on have git submodules in them. Submodules are just a convenient way of including other repositories in another repository. After you clone a repository with submodules in it run git submodule update —init —recursive to initialize all the submodules (basically clones all of them down for you).

Critically, if you make changes to files in a submodule that you want to push back to Github you need to first make sure that you are tracking the proper branch for that repository.

# Enter the directory for the submodule
cd /path/to/submodule
# Change to the branch you want (probably main)
git checkout branch-name
# From here make changes, add, commit, and push