A comprehensive guide covering essential Git commands for version control, collaboration, and repository management.
- Git Configuration
- Repository Management
- Branching & Merging
- Committing Changes
- Undoing Changes
- Working with Remotes
- Tagging
- Stashing
- Rebasing
- Cherry-Picking
- Logging & History
- Submodules
- Git Hooks
Set your Git username:
git config --global user.name "Your Name"Set your Git email:
git config --global user.email "your.email@example.com"Check current configuration:
git config --listInitialize a new repository:
git initClone an existing repository:
git clone <repo-url>List branches:
git branchCreate a new branch:
git branch <branch-name>Switch to a branch:
git checkout <branch-name>Create and switch to a new branch:
git checkout -b <branch-name>Merge a branch into the current branch:
git merge <branch-name>Delete a branch:
git branch -d <branch-name>Check the status of changes:
git statusAdd files to staging:
git add <file>
git add . # Add all changesCommit changes:
git commit -m "Commit message"Amend the last commit:
git commit --amendUnstage a file:
git reset <file>Revert a commit:
git revert <commit-hash>Reset branch to a previous commit:
git reset --hard <commit-hash>List remote repositories:
git remote -vAdd a new remote repository:
git remote add origin <repo-url>Fetch changes from remote:
git fetch originPush changes to remote:
git push origin <branch-name>Pull latest changes from remote:
git pull origin <branch-name>List all tags:
git tagCreate a new tag:
git tag -a <tag-name> -m "Tag message"Push tags to remote:
git push origin --tagsSave uncommitted changes:
git stashApply stashed changes:
git stash popList stashes:
git stash listRebase current branch onto another branch:
git rebase <branch-name>Continue rebase after conflict resolution:
git rebase --continueAbort a rebase:
git rebase --abortApply a specific commit from another branch:
git cherry-pick <commit-hash>View commit history:
git log --oneline --graph --decorate --allView changes in a specific commit:
git show <commit-hash>Add a submodule:
git submodule add <repo-url>Initialize and update submodules:
git submodule update --init --recursiveList available hooks:
ls .git/hooks/Enable a hook (e.g., pre-commit):
chmod +x .git/hooks/pre-commit