Git cheatsheet
Branching
List local branches
git branch
List remote branches
git branch -r
Create new branch
git checkout -b feature/my-change
Switch branch
git checkout main
Delete local branch
git branch -d feature/my-change
Force delete local branch
git branch -D feature/my-change
Staging & Committing
Check status
git status
Stage all changes
git add .
Stage specific file
git add path/to/file.md
Unstage file
git restore --staged file.md
Commit changes
git commit -m "Meaningful message"
Partial staging
Interactive staging
git add -p
Lets you stage only specific parts of a file — perfect when one file contains changes that belong to different branches or commits.
Stage this hunk
y
Add this chunk of changes to the commit.
Skip this hunk
n
Leave this chunk unstaged.
Split hunk into smaller pieces
s
Breaks a large hunk into smaller hunks so you can stage only the lines you want.
This is the key option for your case.
Edit hunk manually
e
Opens the hunk in an editor so you can manually remove or keep specific lines.
Stage all hunks in this file
a
Add all changes in the file.
Skip all hunks in this file
d
Skip all changes in the file.
Syncing with Remote (GitLab)
Pull latest changes
git pull
Push branch to GitLab
git push -u origin feature/my-change
Push after upstream is set
git push
Merging & MR Workflow
Create Merge Request
(done via GitLab UI)
Update branch with main
git pull origin main
Resolve merge conflicts
(edit files → git add . → git commit)
Merge branch into main
(via GitLab UI)
Cleaning Up
Delete remote branch
git push origin --delete feature/my-change
Delete local branch
git branch -d feature/my-change
Useful Everyday Commands
Show commit history
git log --oneline --graph --decorate
Show changes in working directory
git diff
Show staged changes
git diff --staged
Undo last commit (keep changes)
git reset --soft HEAD~1
Undo last commit (discard changes)
git reset --hard HEAD~1
The Workflow
- git checkout main
- git pull
- git checkout -b feature/my-change
- make changes in files
- git add . or git add file/dir
- git commit -m "Meaningful message"
- git push -u origin feature/my-change
- create merge request via GitLab UI
- merge via GitLab UI
- delete branch via GitLab UI
- git checkout main
- git pull
- git branch -d feature/my-change
- repeat