GIT Commands Cheatsheet — Essential Reference
Git is a free and open-source distributed version control system designed to handle everything from small to very large projects with speed and efficiency. It allows multiple developers to work on the same project simultaneously without overwriting each other's changes, tracking every modification made to the codebase.
Master GIT with Top Reference Books
Level up your DevOps skills. Find the best guide books and cheatsheet references for your engineering stack.
Commands
| Command | Description | Example |
|---|---|---|
git init |
Initializes a new Git repository in the current directory. | git init |
git clone [url] |
Clones an existing repository from a URL into a new directory. | git clone https://github.com/octocat/Spoon-Knife.git |
git add [file] / git add . |
Stages changes for the next commit. Use `.` to stage all changes. | git add index.html |
git commit -m "[message]" |
Records the staged changes to the repository with a descriptive message. | git commit -m "Add initial homepage structure" |
git status |
Shows the working tree status, including modified, staged, and untracked files. | git status |
git diff |
Shows changes between the working directory and the staging area. | git diff |
git log |
Displays the commit history. | git log --oneline --graph |
git branch [branch-name] |
Creates a new branch. | git branch feature/new-login |
git checkout [branch-name] / git switch [branch-name] |
Switches to a different branch. `git switch` is a newer, safer alternative. | git checkout develop |
git merge [branch-name] |
Merges changes from the specified branch into the current branch. | git merge feature/new-login |
git pull |
Fetches changes from the remote repository and merges them into the current branch. | git pull origin main |
git push |
Uploads local branch commits to the remote repository. | git push origin main |
git remote -v |
Lists the remote repositories configured for your project. | git remote -v |
git reset [file] / git reset --hard [commit-hash] |
Unstages a file or reverts to a previous commit, discarding subsequent changes. | git reset index.html |
git stash |
Temporarily saves changes that are not ready to be committed. | git stash save "WIP: implementing user profile" |
Pro Tips
- Always commit small, logical changes with clear, descriptive messages.
- Use branches extensively for new features or bug fixes to isolate your work.
- Regularly `git pull` to stay updated with the remote repository and avoid merge conflicts.
- Learn to use `git rebase` for a cleaner, linear commit history, especially before merging.
- Configure your global Git settings (user name, email) with `git config --global user.name "Your Name"` and `git config --global user.email "your.email@example.com"`.
Frequently Asked Questions
What is git used for?
Git is primarily used for version control, allowing individuals and teams to track changes in source code during software development. It enables collaboration, facilitates reverting to previous states, and helps manage different versions of a project.
How do I install git?
On macOS, you can install Git with Homebrew: `brew install git`. On Windows, download the installer from git-scm.com. On Linux, use your distribution's package manager, e.g., `sudo apt-get install git` for Debian/Ubuntu or `sudo yum install git` for Fedora/RHEL.