1. Basic Repository Management
- Initialize a repository:
git init - Clone a repository:
git clone repository_url - Add all changes to staging:
git add . - Commit changes:
git commit -m "commit message" - Push changes to remote:
git push origin branch_name - Pull latest changes:
git pull origin branch_name - View commit history:
git log - View commit history in one line:
git log --oneline
2. Branching and Merging
- Create 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 - Rebase a branch:
git rebase branch_name - Delete a branch:
- Locally:
git branch -d branch_name - Remotely:
git push origin --delete branch_name
- Locally:
3. Collaboration and Remote Management
- Add a remote:
git remote add origin repository_url - List all remotes:
git remote -v - Fetch changes from a remote:
git fetch origin - Pull changes with rebase:
git pull --rebase origin branch_name - Push all local branches:
git push --all origin - View remote branches:
git branch -r - Set tracking for a remote branch:
git branch --set-upstream-to=origin/branch_name
4. Stashing and Cleaning Up
- Stash changes:
git stash - List stashes:
git stash list - Apply the latest stash:
git stash apply - Apply a specific stash:
git stash apply stash@{index} - Pop (apply and delete) stash:
git stash pop - Clean untracked files:
git clean -f - Remove all stashes:
git stash clear
5. Inspecting and Troubleshooting
- Check status:
git status - View staged changes:
git diff --staged - View changes for a specific file:
git diff file_name - Show changes since the last commit:
git diff HEAD - View specific commit details:
git show commit_hash - Inspect branch merge-base:
git merge-base branch1 branch2
6. Tagging and Releases
- Create a tag:
git tag tag_name - Create an annotated tag:
git tag -a tag_name -m "tag message" - Push tags to remote:
git push origin --tags - Delete a tag locally:
git tag -d tag_name - Delete a tag remotely:
git push origin --delete tag_name
7. Advanced History & Blame
- Show commit history for a file:
git log file_name - Blame a file (show line-by-line history):
git blame file_name - Search commit history for keywords:
git log -S "search_term" - View all commits by an author:
git log --author="author_name" - Revert to a specific commit:
git revert commit_hash
8. Rewriting History and Amendments
- Amend the last commit:
git commit --amend -m "new commit message" - Interactive rebase (rewrite commit history):
git rebase -i commit_hash - Edit specific commit:
git rebase -i HEAD~n(replacenwith the number of commits) - Squash commits: Use
git rebase -iand mark commits assquashors
9. Cherry-Picking and Resetting
- Apply a specific commit to another branch:
git cherry-pick commit_hash - Undo changes for a specific file:
git checkout -- file_name - Soft reset (keep changes staged):
git reset --soft commit_hash - Hard reset (discard all changes):
git reset --hard commit_hash - Reset to remote:
git reset --hard origin/branch_name
10. Git Config and Aliases
- Configure user email:
git config --global user.email "your_email@example.com" - Configure user name:
git config --global user.name "Your Name" - Set a Git alias:
git config --global alias.co checkout - List all Git configurations:
git config --list
11. Submodules
- Add a submodule:
git submodule add repository_url path - Update all submodules:
git submodule update --init --recursive - Remove a submodule:
git rm --cached path_to_submodule - Pull updates for all submodules:
git submodule foreach git pull origin master