⬅ Back to Catalogue

Outline#

Whether you’re new to Git or a seasoned developer, having a quick reference at hand is always useful. This Git cheatsheet is designed to give you a concise summary of the most important and commonly used Git commands. It covers everything from creating a repository to handling branches, committing changes, and resolving conflicts. Think of it as your go-to guide when you need a reminder of how to perform a task in Git, without having to dive deep into the documentation.

A PDF cheatsheet is available here.

Good Practices#

  • Focus on learning the core commands: git add, git commit, git push, git pull, and git status. These commands cover most of what you need when starting out.
  • Use git status often! It shows you what’s going on in your repository and helps you understand what changes you’ve made.
  • Make small and frequent commits instead of squeezing everything in a single commit.
  • Also remember to write clear and descriptive commit messages so others (and future you) know what the changes are about.
  • Always create feature branches for different small tasks instead of doing everything in main.
  • It’s a good habit to pull latest changes to make sure your local copy is up to date before you start working on your task.
  • It’s a good idea to rebase your feature branch onto main to eliminate conflicts before you push it for a merge request.
  • Conflicts happen when Git can’t automatically merge changes. Don’t panic! The log will prompt you which files have conflicts.

Linux Commands#

Command Name Usage
man command manual If you don't know how to use a specific command, simply check out the manual of it. e.g. man ls will list the name, description, synopsis and acceptable options for the ls command. It gives you a detailed manual of how to use ls.
pwd print working directory Display the current working directory in the directory (folder) tree, which is the directory at which the terminal is currently positioned. It shows you the absolute path starting from /home.
cd [directory] change directory Changes the working directory to the given directory. ~ is an alias for your home directory. . means the current directory you are at. .. means the last-level directory from where you are at. - means the previous directory you are at.
ls [directory] list Lists the contents of the directory you are currently in.
touch [filename] Create a new file. e.g. touch file.txt.
cat Display the contents of a text file on the screen. e.g.: cat file.txt would display the file we created in the previous section.
cp copy Copy a file from one location to another. e.g. cp file.txt comp1100 copies the file.txt file in the current directory to the "comp1100" directory.
mv move Move a file to a new location or rename it. e.g. mv file.txt text.txt renames "file.txt" to "text.txt" and mv file.txt .. moves "file.txt" to the parent directory.
rm remove Delete a file. e.g. rm file.txt deletes "file.txt". Think twice before deleting!!!.
mkdir make directory Make a new directory. e.g. mkdir comp1100 makes a new directory named "comp1100".
rmdir remove directory Remove a directory. e.g. rmdir comp1100 deletes a directory named "comp1100". Think twice before deleting!!!.
tar tape archive Compress or decompress files. e.g. tar -cvf file.tar *.txt creates a tar file which compresses all TXT files in the current directory; tar -xvf file.tar extracts all files from file.tar into the current directory.

Git Commands#

Command Usage
git init Initialize a git repository. Follow the prompts to create it.
git status Check the current status of your git repository.
git clone [URL] Clone a remote git repository to your local computer. You should select a HTTPS or SSH URL.
git remote add origin [URL] Tell git to associate the name "origin" with the specified remote repository URL. If you've created a local git repository using git init and you want to connect it to a remote repository, you would use it.
git add [filename1] [filename2] ... Add your changes from workspace to the staging area.
git commit Commit your changes in the staging area to the local repository. e.g., git commit -m "created contribution.md"
git push origin [branch] Push your committed changes in a local branch from the local git repository to the remote git repository. Adding -u can set the upstream tracking relationship between your local branch and the remote branch so that you can simply use git push or git pull in the future. You can add -u for your first push.
git pull origin [branch] Pull changes of a branch from your remote git repository to the local repository.
git branch Create a new branch. Adding -d will delete a specific branch instead!
git checkout [branch] Switch to another branch. Adding -b will first create this branch and then switch to it.
git merge [branch] Merge the source branch to the current branch.
git rebase [branch] Rebase the current branch to the target branch.
git log Print out a log of all your previous commits. Adding --oneline can make the log shorter.
git revert [commit-hash] Undo the changes while preserving the history by adding a new commit that undoes the chosen commit.
git reset Undo changes by moving the HEAD (current commit) and potentially alter the state of your files and staged changes. Adding --soft will only move HEAD and thus keep staged changes. Adding --hard will not only move HEAD but also discard all uncommitted changes! Use --hard with caution!
14
bars search caret-down plus minus arrow-right times