Git is a powerful version control system that helps developers manage code changes, collaborate with teams, and maintain project history. Whether you’re a beginner or an experienced developer, this Git cheat sheet will save you time and effort.
Essential Git Commands
1. Repository Setup
| Command | Description |
|---|---|
git init | Initializes a new Git repository in the current directory. |
git clone <repository-url> | Clones a remote repository to your local machine. |
git remote add origin <repository-url> | Adds a remote repository URL (e.g., GitHub) to your local repo. |
git remote -v | Lists all remote repositories linked to your local repo. |
2. Basic Workflow
| Command | Description |
|---|---|
git status | Shows the status of your working directory (untracked, modified, or staged files). |
git add <file> | Stages a specific file for commit. |
git add . | Stages all changes in the working directory. |
git commit -m "commit message" | Commits staged changes with a descriptive message. |
git push origin <branch> | Pushes local commits to a remote repository. |
git pull origin <branch> | Fetches and merges changes from a remote repository to your local branch. |
3. Branching and Merging
| Command | Description |
|---|---|
git branch | Lists all local branches. |
git branch <branch-name> | Creates a new branch. |
git checkout <branch-name> | Switches to the specified branch. |
git checkout -b <branch-name> | Creates and switches to a new branch. |
git merge <branch-name> | Merges the specified branch into the current branch. |
git branch -d <branch-name> | Deletes a local branch. |
git push origin --delete <branch-name> | Deletes a remote branch. |
4. Viewing History and Changes
| Command | Description |
|---|---|
git log | Displays the commit history. |
git log --oneline | Shows a simplified commit history (one line per commit). |
git diff | Shows unstaged changes in the working directory. |
git diff <file> | Shows changes in a specific file. |
git diff <commit1> <commit2> | Compares changes between two commits. |
git show <commit> | Displays details of a specific commit. |
5. Undoing Changes
| Command | Description |
|---|---|
git restore <file> | Discards changes in the working directory for a specific file. |
git restore --staged <file> | Unstages a file but keeps changes in the working directory. |
git reset --hard | Discards all local changes and resets to the last commit. |
git revert <commit> | Creates a new commit that undoes the changes of a specific commit. |
git commit --amend | Modifies the most recent commit (e.g., to update the commit message). |
Advanced Git Commands
1. Stashing Changes
| Command | Description |
|---|---|
git stash | Temporarily saves changes in the working directory. |
git stash list | Lists all stashed changes. |
git stash apply | Applies the most recent stashed changes. |
git stash pop | Applies and removes the most recent stashed changes. |
git stash drop | Deletes the most recent stash. |
2. Rebasing
| Command | Description |
|---|---|
git rebase <branch> | Reapplies commits from the current branch onto another branch. |
git rebase --continue | Continues a rebase after resolving conflicts. |
git rebase --abort | Aborts an ongoing rebase. |
3. Tagging
| Command | Description |
|---|---|
git tag <tag-name> | Creates a lightweight tag for the current commit. |
git tag -a <tag-name> -m "message" | Creates an annotated tag with a message. |
git push origin <tag-name> | Pushes a specific tag to the remote repository. |
git push origin --tags | Pushes all tags to the remote repository. |
4. Submodules
| Command | Description |
|---|---|
git submodule add <repository-url> | Adds a submodule to your repository. |
git submodule update --init --recursive | Initializes and updates submodules. |
Common Git Challenges and Solutions
| Challenge | Solution |
|---|---|
| Accidentally committed to the wrong branch | Use git stash to save changes, switch to the correct branch, and apply the stash. |
| Merge conflicts | Resolve conflicts manually in the affected files, then use git add and git commit to complete the merge. |
| Lost commit history | Use git reflog to find the lost commit and reset to it. |
| Large files in the repository | Use git filter-branch or tools like BFG Repo-Cleaner to remove large files from history. |
FAQs About Git
What is the difference between
git pull and git fetch? git fetch downloads changes from the remote repository but does not merge them.git pull downloads changes and automatically merges them into your current branch.
How do I squash multiple commits into one?
Use git rebase -i HEAD~<number-of-commits> and mark commits as squash in the interactive editor.
Can I recover a deleted branch?
Yes, use git reflog to find the commit hash of the deleted branch and then run git checkout -b <branch-name> <commit-hash>.
How do I ignore files in Git?
Create a .gitignore file in your repository and list the files or patterns you want to ignore.
Pro Tips for Using Git
- Write meaningful commit messages: Use clear and concise messages to describe changes.
- Use branches for new features: Always create a new branch for feature development to avoid disrupting the main branch.
- Regularly pull changes: Sync your local repository with the remote repository to avoid merge conflicts.
- Leverage Git hooks: Automate tasks like linting or testing using Git hooks.