-
Create and switch to feature branch:
git checkout -b feature/new-feature
-
Merge feature branch into main:
git checkout main git merge feature/new-feature
-
Handle merge conflicts:
git status # Check which files have conflicts git diff # View the conflicts # After manually resolving conflicts: git add . git commit -m "Resolved merge conflicts"
-
Delete feature branch after merging:
git branch -d feature/new-feature # Local branch deletion git push origin --delete feature/new-feature # Remote branch deletion
-
Update feature branch with main changes:
git checkout feature/new-feature git rebase main
-
Save current changes to stash:
git stash save "work in progress on feature X" -
List all stashes:
git stash list
-
Apply most recent stash:
git stash apply # Keeps stash in list git stash pop # Removes stash after applying
-
Apply specific stash:
git stash apply stash@{n} # n is stash index number -
Create branch from stash:
git stash branch new-branch stash@{n} -
Remove stashes:
git stash drop stash@{n} # Remove specific stash git stash clear # Remove all stashes
-
View commit history:
git log # Full commit history git log --oneline # Compact view git log --graph --oneline # Visual commit tree
-
Search commit history:
git log --grep="keyword" # Search commit messages git log -S"code string" # Search for code changes git log --author="name" # Search by author
-
Compare changes:
git diff commit1..commit2 # Changes between commits git diff branch1..branch2 # Changes between branches git show commit_hash # View specific commit changes
-
View file history:
git log --follow filename # Track file history through renames git blame filename # See who changed each line
-
Working with multiple remotes:
git remote -v # List all remotes git remote add upstream repository-url # Add new remote git remote rename origin new-name # Rename remote git remote remove remote-name # Remove remote
-
Advanced remote operations:
git fetch --all # Update all remotes git remote prune origin # Clean up deleted remote branches git push --all origin # Push all branches
-
Remote branch management:
git branch -r # List remote branches git checkout -t origin/branch-name # Track remote branch git push origin --delete branch-name # Delete remote branch
-
Syncing with upstream:
git fetch upstream # Get upstream changes git merge upstream/main # Merge upstream changes git rebase upstream/main # Rebase on upstream
-
Global user settings:
git config --global user.name "Your Name" git config --global user.email "your.email@example.com" git config --list # View all settings
-
SSH key setup:
ssh-keygen -t ed25519 -C "your.email@example.com" ssh-add ~/.ssh/id_ed25519
-
Create useful aliases:
git config --global alias.st status git config --global alias.co checkout git config --global alias.br branch git config --global alias.ci commit
-
Set default behaviors:
git config --global core.editor "code --wait" # Set VS Code as editor git config --global pull.rebase true # Auto rebase on pull git config --global core.autocrlf true # Line ending handling