Overview
This is a summary of the most important Git commands that you can use in Git Bash. If you’re not so familiar with working in the command prompt/terminal, you could also try to check out Git Desktop or Git GUI, which provides a graphical user interface for performing the Git workflow.
The building block consists of the following sections:
- The most important Git commands
- Advanced use cases
- Add all files to the staging area
- Ignore files from versioning
- Undo previous commits
The most important Git commands
Clone (“download”) the repository to your computer.
Do not clone a repository into another repository!
Check which files/directories have changed since your last commit.
Add specified file(s) to the staging area, so that any changes can eventually be committed.
Commit staged changes to the version history of your repository.
It’s good practice to use a clear & concise commit message (“note to your future self and others”) which shows up prominently on your repository at GitHub.com.
Push any changes you have done to the repository on your computer to the specified branch of the remote repository (e.g., at GitHub.com).
View your commit history (i.e., a list of commit IDs and your commit messages).
Advanced use cases
Add all files to the staging area
To add all files that have been changed to the staging area (to eventually commit them), use git add .
. That way, you don’t have to mention files individually.
Beware to not accidentally version files that must not be versioned (e.g., datasets) when using git add .
.
Ignore files from versioning
You can create a .gitignore
file (e.g., in the root directory of your repository) to tell Git to stop paying attention to files you don’t want to version (e.g., datasets, operation-specific files like Mac’s .DS_Store
or R’s .RHistory
.
For example, if you save the snippet below in a file called .gitignore
, any content in the my_passwords
folder and any .csv
files will be ignored - even when using git add .
!
Undo previous commits
It happens to all of us - we sometimes commit stuff that we didn’t mean to commit. That can be quite problematic, for example, if you’ve accidentally committed a password. But even for less drastic cases, reverting “wrong” commits is good practice because it keeps your repository clean.
The snippet below undoes the last commit.
Alternatively, you can view the commit history with git log
, and revert to any commit by referring to its unique id (which looks a bit like this: 0hf1u7x2
).
Useful links
To learn more about Git commands, these links might be useful:
- Official GitHub Cheat Sheet
- Oh Shit, Git!?! A collection of advanced and useful Git commands!
- Version control on The Turing Way
- Version control at Software Carpentry
- Installation guide for Git and GitHub
This building block provides a summary of the most important Git commands and advanced use cases in Git Bash. Bookmark this page for quick access whenever you need a quick reminder!