Skip to content

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

  1. git checkout main
  2. git pull
  3. git checkout -b feature/my-change
  4. make changes in files
  5. git add . or git add file/dir
  6. git commit -m "Meaningful message"
  7. git push -u origin feature/my-change
  8. create merge request via GitLab UI
  9. merge via GitLab UI
  10. delete branch via GitLab UI
  11. git checkout main
  12. git pull
  13. git branch -d feature/my-change
  14. repeat