Git Commands Cheatsheet

Quick reference for common Git commands used in EBRAINS development.

Repository Setup

CommandDescriptionExample
git clone <url>Clone repositorygit clone git@gitlab.ebrains.eu:ri/tech-hub/ebrains-ui-ecosystem.git
git initInitialize new repositorygit init
git remote add origin <url>Add remote repositorygit remote add origin git@gitlab.ebrains.eu:user/repo.git
git remote -vList remote repositoriesgit remote -v

Branching

CommandDescriptionExample
git branchList local branchesgit branch
git branch -aList all branches (local + remote)git branch -a
git checkout -b <branch>Create and switch to branchgit checkout -b feat/18-add-feature
git switch -c <branch>Create and switch to branch (Git 2.23+)git switch -c feat/18-add-feature
git checkout <branch>Switch to existing branchgit checkout main
git switch <branch>Switch to existing branch (Git 2.23+)git switch main
git branch -d <branch>Delete local branch (safe)git branch -d feat/18-add-feature
git branch -D <branch>Force delete local branchgit branch -D feat/18-add-feature
git push origin --delete <branch>Delete remote branchgit push origin --delete feat/18

Status and Differences

CommandDescriptionExample
git statusShow working tree statusgit status
git diffShow unstaged changesgit diff
git diff --stagedShow staged changesgit diff --staged
git diff mainCompare with another branchgit diff main
git logShow commit historygit log
git log --onelineShow compact commit historygit log --oneline
git log --oneline -5Show last 5 commitsgit log --oneline -5
git log --graphShow commit graphgit log --graph --oneline

Staging and Committing

CommandDescriptionExample
git add <file>Stage specific filegit add src/app.ts
git add .Stage all changesgit add .
git add -pStage changes interactivelygit add -p
git reset <file>Unstage filegit reset src/app.ts
git resetUnstage all filesgit reset
git commit -m "<msg>"Commit with messagegit commit -m "feat: add feature"
git commit -m "<msg>" -m "<body>"Commit with bodygit commit -m "feat: add feature" -m "Description"
git commit --amendAmend last commitgit commit --amend
git commit -F <file>Commit with message from filegit commit -F commit-msg.txt

EBRAINS Commit Format

CommandExample
Simple commitgit commit -m "feat(forms): add validation"
With bodygit commit -m "feat(api): add endpoint" -m "Added POST /api/users endpoint"
With trailergit commit -m "fix: correct bug" -m "Fixed date issue" -m "Changelog: fixed"
Breaking changegit commit -m "feat!: change API" -m "BREAKING CHANGE: New format" -m "Changelog: changed"

Synchronizing

CommandDescriptionExample
git fetchDownload objects from remotegit fetch origin
git pullFetch and merge from remotegit pull origin main
git pushPush to remotegit push origin feat/18
git push -u origin <branch>Push and set upstreamgit push -u origin feat/18
git push origin --tagsPush all tagsgit push origin --tags
git push --force-with-leaseForce push (safer)git push --force-with-lease origin feat/18

Tagging

CommandDescriptionExample
git tagList all tagsgit tag
git tag -l "v1.*"List tags matching patterngit tag -l "v1.*"
git tag <version>Create lightweight taggit tag v1.0.0
git tag -a <version> -m "<msg>"Create annotated taggit tag -a v1.0.0 -m "Release 1.0.0"
git show <version>Show tag detailsgit show v1.0.0
git push origin <version>Push single taggit push origin v1.0.0
git push --tagsPush all tagsgit push --tags
git tag -d <version>Delete local taggit tag -d v1.0.0
git push origin --delete <version>Delete remote taggit push origin --delete v1.0.0

Merging and Rebasing

CommandDescriptionExample
git merge <branch>Merge branch into currentgit merge main
git merge --no-ff <branch>Merge without fast-forwardgit merge --no-ff feat/18
git rebase <branch>Rebase current branchgit rebase main
git rebase --continueContinue after resolving conflictsgit rebase --continue
git rebase --abortAbort rebasegit rebase --abort
git cherry-pick <commit>Apply specific commitgit cherry-pick abc123

Stashing

CommandDescriptionExample
git stashStash current changesgit stash
git stash save "<msg>"Stash with messagegit stash save "WIP: feature"
git stash listList all stashesgit stash list
git stash popApply and remove latest stashgit stash pop
git stash applyApply latest stash (keep it)git stash apply
git stash dropDelete latest stashgit stash drop
git stash clearDelete all stashesgit stash clear

Undoing Changes

CommandDescriptionExample
git restore <file>Discard changes in filegit restore src/app.ts
git restore --staged <file>Unstage filegit restore --staged src/app.ts
git checkout -- <file>Discard changes (old syntax)git checkout -- src/app.ts
git reset --soft HEAD~1Undo last commit, keep changes stagedgit reset --soft HEAD~1
git reset --mixed HEAD~1Undo last commit, keep changes unstagedgit reset --mixed HEAD~1
git reset --hard HEAD~1Undo last commit, discard changesgit reset --hard HEAD~1
git revert <commit>Create new commit that undoes changesgit revert abc123

Configuration

CommandDescriptionExample
git config --global user.name "<name>"Set usernamegit config --global user.name "John Doe"
git config --global user.email "<email>"Set emailgit config --global user.email "john@example.com"
git config --listList all configurationsgit config --list
git config user.nameShow usernamegit config user.name

Inspection

CommandDescriptionExample
git show <commit>Show commit detailsgit show abc123
git blame <file>Show who changed each linegit blame src/app.ts
git log --author="<name>"Show commits by authorgit log --author="John"
git log --since="2 weeks ago"Show recent commitsgit log --since="2 weeks ago"
git log --grep="<pattern>"Search commit messagesgit log --grep="fix"
git reflogShow reference loggit reflog

Cleaning

CommandDescriptionExample
git clean -nShow what would be deleted (dry run)git clean -n
git clean -fDelete untracked filesgit clean -f
git clean -fdDelete untracked files and directoriesgit clean -fd
git clean -fXDelete ignored filesgit clean -fX
git clean -fxDelete untracked and ignored filesgit clean -fx

Common Workflows

Start New Feature

Commit and Push

Update Feature Branch with Main

Fix Last Commit Message

Create Release Tag

Delete Branch After Merge

Troubleshooting

Undo Last Commit (Keep Changes)

Discard All Local Changes

Resolve Merge Conflicts

Recover Deleted Commit

Related Documentation