Reading time: 7 minutes.
Version control is a critical skill in modern software development, and Git is the most widely used version control system today. Whether you’re a beginner or an experienced developer, understanding Git commands and workflows can significantly improve your productivity and collaboration with other developers. This cheat sheet is designed to give you a comprehensive overview of essential Git commands, concepts, and best practices. By the end of this guide, you’ll have a handy reference to help you navigate through your Git workflows with ease.
Download the PDF version of the GIT Cheat Sheet
Table of Contents
- Getting Started with Git
- Installing Git
- Configuring Git
- Understanding Repositories
- Basic Git Commands
- Initializing a Repository
- Cloning a Repository
- Checking the Status
- Adding and Committing Changes
- Viewing Commit History
- Branching and Merging
- Creating Branches
- Switching Branches
- Merging Branches
- Resolving Merge Conflicts
- Remote Repositories
- Adding a Remote Repository
- Pushing Changes
- Pulling Changes
- Fetching Changes
- Working with Multiple Remotes
- Advanced Git Commands
- Stashing Changes
- Reverting Changes
- Resetting Commits
- Rebasing Branches
- Cherry-picking Commits
- Git Workflows
- Git Flow
- Forking Workflow
- Feature Branch Workflow
- Best Practices and Tips
- Writing Good Commit Messages
- Avoiding Common Pitfalls
- Useful Aliases and Shortcuts
- Keeping a Clean History
- Git Tools and Resources
- Git GUIs
- Online Git Repositories
- GitHub Actions and CI/CD
- Learning Resources
1. Getting Started with Git
Installing Git
Before you can use Git, you need to install it on your computer. Git is available for all major operating systems:
- Windows: Download the installer from git-scm.com and follow the setup instructions.
- macOS: Install Git via Homebrew with the command
brew install git
, or download the installer from git-scm.com. - Linux: Install Git using your distribution’s package manager. For example, on Ubuntu, run
sudo apt-get install git
.
Configuring Git
After installation, you should configure Git with your username and email, as these will be associated with your commits:
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
You can also set up other preferences, such as your default text editor:
git config --global core.editor "nano"
Understanding Repositories
A Git repository is a directory that tracks changes in your project files. It contains all the information needed to manage the project’s history, including commits, branches, and tags. Repositories can be either local (on your machine) or remote (hosted on services like GitHub, GitLab, or Bitbucket).
2. Basic Git Commands
Initializing a Repository
To start tracking a new project with Git, navigate to your project directory and run:
git init
This command creates a new .git
directory that stores all the version control information.
Cloning a Repository
To clone an existing repository (copy it to your local machine), use:
git clone <repository-url>
For example:
git clone https://github.com/username/repository.git
Checking the Status
To see the current state of your working directory and staging area, run:
git status
This command shows which files have been modified, staged for commit, or are untracked by Git.
Adding and Committing Changes
To add changes to the staging area, use:
git add <file-name>
Or, to add all changes:
git add .
Once your changes are staged, you can commit them to the repository:
git commit -m "Your commit message"
Viewing Commit History
To view the history of commits, use:
git log
For a more concise and graphical view:
git log --oneline --graph --all
3. Branching and Merging
Creating Branches
Branches allow you to work on different features or fixes independently. To create a new branch, run:
git branch <branch-name>
Or, to create and switch to the new branch immediately:
git checkout -b <branch-name>
Switching Branches
To switch to an existing branch, use:
git checkout <branch-name>
Merging Branches
Once your work on a branch is complete, you can merge it back into the main branch (usually main
or master
):
git checkout main
git merge <branch-name>
Resolving Merge Conflicts
If two branches have changes in the same part of a file, Git will generate a conflict during the merge. To resolve conflicts, open the affected files and decide which changes to keep. After resolving, mark the conflicts as resolved:
git add <resolved-file>
git commit -m "Resolved merge conflict"
4. Remote Repositories
Adding a Remote Repository
To link your local repository to a remote one, use:
git remote add origin <remote-url>
You can verify the remote with:
git remote -v
Pushing Changes
To upload your local commits to the remote repository, use:
git push origin <branch-name>
If you’re pushing a new branch for the first time:
git push -u origin <branch-name>
Pulling Changes
To update your local branch with changes from the remote repository, use:
git pull origin <branch-name>
Fetching Changes
Fetching updates the local copy of the remote repository without merging the changes:
git fetch origin
Working with Multiple Remotes
If you collaborate on a project with multiple repositories, you can add additional remotes:
git remote add upstream <upstream-url>
This is useful when working with forks.
5. Advanced Git Commands
Stashing Changes
If you need to switch branches but have uncommitted changes, you can temporarily save them with:
git stash
To reapply the stashed changes later:
git stash apply
Reverting Changes
To revert a specific commit (create a new commit that undoes the changes):
git revert <commit-hash>
Resetting Commits
To undo commits without keeping a history (use with caution):
git reset --hard <commit-hash>
Rebasing Branches
Rebasing allows you to move or combine commits from one branch onto another:
git rebase <branch-name>
Cherry-picking Commits
Cherry-picking allows you to apply a commit from one branch to another:
git cherry-pick <commit-hash>
6. Git Workflows
Git Flow
Git Flow is a popular branching strategy for managing releases. It involves creating branches for new features, releases, and hotfixes. The main branches in Git Flow are main
(for stable releases) and develop
(for ongoing development).
Forking Workflow
This workflow is common in open-source projects. Developers fork the main repository, work on their copy, and then submit pull requests to merge their changes back into the original repository.
Feature Branch Workflow
The feature branch workflow involves creating a new branch for each feature or bug fix. Once the work is complete, the branch is merged back into the main branch. This keeps the main branch clean and stable.
7. Best Practices and Tips
Writing Good Commit Messages
A good commit message should be concise and descriptive. It should explain what the change does and why it was made. A common format is:
Short summary (50 characters or less)
Detailed explanation, if necessary (72 characters per line)
Avoiding Common Pitfalls
- Avoid committing large files: Use
.gitignore
to exclude unnecessary files. - Don’t rewrite public history: Avoid using
git rebase
on shared branches. - Commit often: Make small, frequent commits to keep your work organized.
Useful Aliases and Shortcuts
You can create aliases for commonly used commands:
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.st status
Keeping a Clean History
To keep a clean history, consider using git rebase
instead of git merge
for simple changes, and use squash
to combine multiple commits into one.
8. Git Tools and Resources
Git GUIs
If you prefer a graphical interface over the command line, there are several Git GUIs available:
- GitHub Desktop: Simplifies the Git workflow for GitHub users.
- Sourcetree: A free Git client for Windows and macOS.
- GitKraken: A cross-platform Git GUI with a focus on usability.
Online Git Repositories
Popular platforms for hosting Git repositories include:
- GitHub: The largest Git hosting service, widely used for open-source and private projects.
- GitLab: Offers built-in CI/CD, issue tracking, and more.
- Bitbucket: Supports both Git and Mercurial, with a focus on teams.
GitHub Actions and CI/CD
GitHub Actions allows you to automate workflows directly from your GitHub repository. You can set up Continuous Integration/Continuous Deployment (CI/CD) pipelines to automatically test and deploy your code.
Learning Resources
To further enhance your Git skills, consider the following resources:
- Pro Git (Book): A comprehensive guide to Git.
- Git Documentation: The official Git documentation is thorough and detailed.
- Online Courses: Platforms like Udemy, Coursera, and freeCodeCamp offer courses on Git.
Conclusion
This Git cheat sheet provides a comprehensive overview of the most commonly used Git commands and workflows. Whether you’re working on a solo project or collaborating with a team, mastering Git can help you manage your codebase efficiently and avoid common pitfalls. Keep this guide handy as you continue to work with Git, and remember that practice is key to becoming proficient with version control.
Happy coding!