Complete Git reference — branching, rebasing, stashing, undoing mistakes, and resolving merge conflicts every engineer hits in production repos.
Run these once per machine before your first commit.
git config --global user.name "Aditi Sharma"
git config --global user.email "aditi@razorpay.com"
git config --global init.defaultBranch main
git config --global core.editor "vim"
git config --list ## view all active config
git config --global alias.st status ## create a shorthand alias
Parameter Breakdown:
--global: Applies to every repo for this user — drop it to scope to one repoinit.defaultBranch: Sets the default branch name for new repos to mainalias.st: Lets you type git st instead of git statusConnect a local repo to its source of truth.
git clone git@github.com:razorpay/orders-api.git
git remote -v ## list configured remotes
git remote add upstream \
git@github.com:razorpay/orders-api-fork.git
git remote set-url origin \
git@github.com:razorpay/orders-api-v2.git
git fetch origin ## download without merging
Parameter Breakdown:
remote -v: Shows fetch and push URLs for every configured remotefetch: Updates local refs without touching your working directoryremote add upstream: Standard pattern when working from a forkThe daily-driver commands for feature work.
git branch ## list local branches
git branch -a ## include remote branches
git switch -c feature/payment-retry ## create and switch
git switch main ## switch back to main
git branch -d feature/payment-retry ## delete merged branch
git branch -D feature/payment-retry ## force delete unmerged
git push origin --delete feature/payment-retry
Command Parameter Table:
| Flag | Description |
|---|---|
switch -c |
Create a new branch and switch to it in one step |
branch -d |
Safe delete — fails if branch has unmerged commits |
branch -D |
Force delete regardless of merge status |
Build clean, reviewable commits.
git status ## see staged/unstaged changes
git add src/payments/retry.py ## stage one file
git add -p ## stage interactively, hunk by hunk
git commit -m "Add retry logic for failed payment webhooks"
git commit --amend ## edit the last commit
git commit --amend --no-edit ## add staged changes, keep message
Parameter Breakdown:
add -p: Walks through each change hunk and lets you stage selectivelycommit --amend: Rewrites the last commit — never use on already-pushed shared history--no-edit: Keeps the existing commit message when amendingSync with the team's shared history.
git pull origin main ## fetch + merge
git pull --rebase origin main ## fetch + rebase, cleaner history
git push origin feature/payment-retry
git push -u origin feature/payment-retry ## set upstream tracking
git push --force-with-lease ## safer force push
Parameter Breakdown:
pull --rebase: Avoids unnecessary merge commits when syncing a feature branch-u: Sets the upstream so future git push/git pull need no arguments--force-with-lease: Refuses to overwrite if someone else pushed since your last fetch — always prefer this over --forceThe commands that save you from a bad afternoon.
git restore src/payments/retry.py ## discard unstaged changes
git restore --staged src/payments/retry.py ## unstage, keep edits
git reset --soft HEAD~1 ## undo last commit, keep changes staged
git reset --hard HEAD~1 ## undo last commit, discard changes
git revert a1b2c3d ## create a new commit undoing a1b2c3d
git checkout a1b2c3d -- src/file.py ## restore one file from old commit
Command Parameter Table:
| Flag | Description |
|---|---|
reset --soft |
Keeps changes staged — safe for local-only commits |
reset --hard |
Destroys changes — only use on commits not yet pushed |
revert |
Safe on shared history — adds a new commit instead of rewriting |
Park work in progress without committing it.
git stash ## stash tracked changes
git stash push -m "WIP: retry backoff tuning"
git stash list ## view all stashes
git stash apply stash@{1} ## reapply without removing from list
git stash pop ## reapply and remove from list
git stash drop stash@{0} ## delete a specific stash
Parameter Breakdown:
stash push -m: Labels the stash so stash list is actually readable laterapply vs pop: apply keeps the stash entry, pop removes it after restoringClean up history before merging into shared branches.
git rebase main ## replay commits onto latest main
git rebase -i HEAD~3 ## interactive rebase, last 3 commits
git rebase --continue ## after resolving a conflict
git rebase --abort ## bail out entirely
git cherry-pick a1b2c3d ## apply one commit onto current branch
Parameter Breakdown:
rebase -i: Opens an editor to squash, reword, or reorder commitscherry-pick: Pulls a single commit from another branch — useful for hotfix backportsMark release points in history.
git tag v2.3.1 ## lightweight tag
git tag -a v2.3.1 -m "Release 2.3.1" ## annotated tag with message
git push origin v2.3.1 ## push a single tag
git push origin --tags ## push all local tags
git tag -d v2.3.1 ## delete a local tag
Parameter Breakdown:
tag -a: Annotated tags store author, date, and message — preferred for releasesgit push — they need an explicit pushFind out who changed what, and when.
git log --oneline --graph --all ## compact visual history
git log -p src/payments/retry.py ## full diff per commit for a file
git log --author="Aditi Sharma"
git blame src/payments/retry.py ## who last touched each line
git diff main..feature/payment-retry ## compare two branches
git show a1b2c3d ## full details of one commit
Command Parameter Table:
| Flag | Description |
|---|---|
--oneline --graph |
Compact, readable branch visualization |
blame |
Line-by-line authorship — useful before changing legacy code |
diff main..branch |
Shows what a branch adds relative to main |
What to actually do when Git stops you mid-merge.
git merge feature/payment-retry
## CONFLICT (content): Merge conflict in src/payments/retry.py
git status ## see which files are conflicted
## edit the file, resolve the markers, then:
git add src/payments/retry.py ## mark as resolved
git commit ## complete the merge
git merge --abort ## bail out and return to pre-merge state
Notes:
git diff while a conflict is open to see exactly what each side changedgit log --merge shows only the commits relevant to the current conflictCommon shortcuts worth adding to your global config.
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.last 'log -1 HEAD'
git config --global alias.unstage 'restore --staged'
## Find branches already merged into main
git branch --merged main
## Delete all local branches already merged into main
git branch --merged main | grep -v "main" | xargs git branch -d