A comprehensive and easy-to-use Git cheat sheet to help you quickly reference the most common Git commands. Each command is accompanied by a brief description and an example, making it perfect for both beginners and experienced developers.
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
git config --list
git init
git clone https://github.com/username/repository.git
git status
# Add a specific file
git add filename.txt
# Add all changes
git add .
git commit -m "Your commit message"
git push origin main
git pull origin main
git branch
git branch new-branch
git checkout branch-name
git checkout -b new-branch
git merge branch-name
git branch -d branch-name
git remote add origin https://github.com/username/repository.git
git remote -v
git remote remove origin
git log
git log --oneline
# Show differences between working directory and staging area
git diff
# Show differences between two branches
git diff branch1 branch2
git stash
git stash apply
git stash list
git stash drop
git reset HEAD filename.txt
git reset --soft commit_hash
git reset --hard commit_hash
git checkout -- filename.txt
git rm filename.txt
git rm --cached filename.txt
git tag tag-name
git tag -a tag-name -m "Tag message"
git push origin tag-name
git fetch origin
git rebase branch-name
git rebase --abort
Here are the remaining Git commands organized into proper categories and formatted to continue from the previous cheat sheet:
git tag -d tag-name
How it works: This command deletes a tag locally from your repository. The tag will still exist on the remote until you delete it there as well.
git push origin --tags
How it works: The git push --tags
command pushes all local tags to the remote repository, making them available to everyone.
git push origin --delete tag tag-name
How it works: This command removes a tag from the remote repository so that it is no longer available to others.
git clean -f
How it works: The git clean
command removes untracked files from the working directory, helping you clean up unnecessary files. Use -f
to force the deletion.
git clean -n
How it works: This command performs a dry run, showing you which untracked files would be deleted without actually deleting them.
git commit --amend -m "Updated commit message"
How it works: The git commit --amend
command allows you to modify the most recent commit. You can change the commit message or add new changes.
git rebase -i commit_hash
How it works: This command opens an interactive rebase session, where you can modify, combine, or delete previous commits.
git cherry-pick commit_hash
How it works: The git cherry-pick
command allows you to apply changes from a specific commit on one branch to another branch.
git status
How it works: After a merge conflict, running git status
will show the files that have conflicts. You can then manually resolve them.
git add filename.txt
How it works: After resolving conflicts in a file, add the file to the staging area with git add
to mark it as resolved.
git merge --continue
How it works: This command continues the merge process after you've resolved any conflicts.
git merge --abort
How it works: This command aborts a merge in progress and returns the repository to its previous state before the merge started.
git rebase -i HEAD~n
How it works: The git rebase -i
command lets you combine several commits into one. Replace n
with the number of commits you'd like to squash.
git bisect start
git bisect bad
git bisect good commit_hash
How it works: The git bisect
command performs a binary search to find which commit introduced a bug. Mark the bad and good commits, and Git will help you locate the culprit.
git commit -S -m "Signed commit message"
How it works: This command signs your commits with a GPG key, ensuring their authenticity and integrity.
git log --show-signature
How it works: This command checks the signatures of past commits, confirming whether they were signed and valid.
git config --global alias.co checkout
How it works: The git config
command lets you create shortcuts for frequently used Git commands. Here, git co
becomes shorthand for git checkout
.
git config --get-regexp alias
How it works: This command lists all your Git aliases, showing you the shortcuts you've set up for quicker workflows.
git submodule add https://github.com/username/repository.git
How it works: The git submodule add
command includes another Git repository as a submodule within your project, allowing you to track it as a separate module.
git submodule init
How it works: This command initializes all submodules in your project after they have been cloned.
git submodule update
How it works: The git submodule update
command updates all submodules to their latest commits, ensuring they are in sync with the parent project.
# Navigate to .git/hooks/ directory
cd .git/hooks/
# Create a new pre-commit hook
touch pre-commit
How it works: This command creates a pre-commit hook script in the .git/hooks/
directory. Pre-commit hooks run before each commit to check code quality or perform other tasks.
This Git cheat sheet covers the essential commands you'll need for most workflows. Keep it handy as a quick reference guide, and enhance your Git skills with ease! If you need more advanced commands or explanations, don't hesitate to dive deeper into the Git documentation or explore additional resources.