Git Commands Cheatsheet
Quick reference for common Git commands used in EBRAINS development.
Repository Setup
| Command | Description | Example |
|---|---|---|
git clone <url> | Clone repository | git clone git@gitlab.ebrains.eu:ri/tech-hub/ebrains-ui-ecosystem.git |
git init | Initialize new repository | git init |
git remote add origin <url> | Add remote repository | git remote add origin git@gitlab.ebrains.eu:user/repo.git |
git remote -v | List remote repositories | git remote -v |
Branching
| Command | Description | Example |
|---|---|---|
git branch | List local branches | git branch |
git branch -a | List all branches (local + remote) | git branch -a |
git checkout -b <branch> | Create and switch to branch | git 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 branch | git 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 branch | git branch -D feat/18-add-feature |
git push origin --delete <branch> | Delete remote branch | git push origin --delete feat/18 |
Status and Differences
| Command | Description | Example |
|---|---|---|
git status | Show working tree status | git status |
git diff | Show unstaged changes | git diff |
git diff --staged | Show staged changes | git diff --staged |
git diff main | Compare with another branch | git diff main |
git log | Show commit history | git log |
git log --oneline | Show compact commit history | git log --oneline |
git log --oneline -5 | Show last 5 commits | git log --oneline -5 |
git log --graph | Show commit graph | git log --graph --oneline |
Staging and Committing
| Command | Description | Example |
|---|---|---|
git add <file> | Stage specific file | git add src/app.ts |
git add . | Stage all changes | git add . |
git add -p | Stage changes interactively | git add -p |
git reset <file> | Unstage file | git reset src/app.ts |
git reset | Unstage all files | git reset |
git commit -m "<msg>" | Commit with message | git commit -m "feat: add feature" |
git commit -m "<msg>" -m "<body>" | Commit with body | git commit -m "feat: add feature" -m "Description" |
git commit --amend | Amend last commit | git commit --amend |
git commit -F <file> | Commit with message from file | git commit -F commit-msg.txt |
EBRAINS Commit Format
| Command | Example |
|---|---|
| Simple commit | git commit -m "feat(forms): add validation" |
| With body | git commit -m "feat(api): add endpoint" -m "Added POST /api/users endpoint" |
| With trailer | git commit -m "fix: correct bug" -m "Fixed date issue" -m "Changelog: fixed" |
| Breaking change | git commit -m "feat!: change API" -m "BREAKING CHANGE: New format" -m "Changelog: changed" |
Synchronizing
| Command | Description | Example |
|---|---|---|
git fetch | Download objects from remote | git fetch origin |
git pull | Fetch and merge from remote | git pull origin main |
git push | Push to remote | git push origin feat/18 |
git push -u origin <branch> | Push and set upstream | git push -u origin feat/18 |
git push origin --tags | Push all tags | git push origin --tags |
git push --force-with-lease | Force push (safer) | git push --force-with-lease origin feat/18 |
Tagging
| Command | Description | Example |
|---|---|---|
git tag | List all tags | git tag |
git tag -l "v1.*" | List tags matching pattern | git tag -l "v1.*" |
git tag <version> | Create lightweight tag | git tag v1.0.0 |
git tag -a <version> -m "<msg>" | Create annotated tag | git tag -a v1.0.0 -m "Release 1.0.0" |
git show <version> | Show tag details | git show v1.0.0 |
git push origin <version> | Push single tag | git push origin v1.0.0 |
git push --tags | Push all tags | git push --tags |
git tag -d <version> | Delete local tag | git tag -d v1.0.0 |
git push origin --delete <version> | Delete remote tag | git push origin --delete v1.0.0 |
Merging and Rebasing
| Command | Description | Example |
|---|---|---|
git merge <branch> | Merge branch into current | git merge main |
git merge --no-ff <branch> | Merge without fast-forward | git merge --no-ff feat/18 |
git rebase <branch> | Rebase current branch | git rebase main |
git rebase --continue | Continue after resolving conflicts | git rebase --continue |
git rebase --abort | Abort rebase | git rebase --abort |
git cherry-pick <commit> | Apply specific commit | git cherry-pick abc123 |
Stashing
| Command | Description | Example |
|---|---|---|
git stash | Stash current changes | git stash |
git stash save "<msg>" | Stash with message | git stash save "WIP: feature" |
git stash list | List all stashes | git stash list |
git stash pop | Apply and remove latest stash | git stash pop |
git stash apply | Apply latest stash (keep it) | git stash apply |
git stash drop | Delete latest stash | git stash drop |
git stash clear | Delete all stashes | git stash clear |
Undoing Changes
| Command | Description | Example |
|---|---|---|
git restore <file> | Discard changes in file | git restore src/app.ts |
git restore --staged <file> | Unstage file | git restore --staged src/app.ts |
git checkout -- <file> | Discard changes (old syntax) | git checkout -- src/app.ts |
git reset --soft HEAD~1 | Undo last commit, keep changes staged | git reset --soft HEAD~1 |
git reset --mixed HEAD~1 | Undo last commit, keep changes unstaged | git reset --mixed HEAD~1 |
git reset --hard HEAD~1 | Undo last commit, discard changes | git reset --hard HEAD~1 |
git revert <commit> | Create new commit that undoes changes | git revert abc123 |
Configuration
| Command | Description | Example |
|---|---|---|
git config --global user.name "<name>" | Set username | git config --global user.name "John Doe" |
git config --global user.email "<email>" | Set email | git config --global user.email "john@example.com" |
git config --list | List all configurations | git config --list |
git config user.name | Show username | git config user.name |
Inspection
| Command | Description | Example |
|---|---|---|
git show <commit> | Show commit details | git show abc123 |
git blame <file> | Show who changed each line | git blame src/app.ts |
git log --author="<name>" | Show commits by author | git log --author="John" |
git log --since="2 weeks ago" | Show recent commits | git log --since="2 weeks ago" |
git log --grep="<pattern>" | Search commit messages | git log --grep="fix" |
git reflog | Show reference log | git reflog |
Cleaning
| Command | Description | Example |
|---|---|---|
git clean -n | Show what would be deleted (dry run) | git clean -n |
git clean -f | Delete untracked files | git clean -f |
git clean -fd | Delete untracked files and directories | git clean -fd |
git clean -fX | Delete ignored files | git clean -fX |
git clean -fx | Delete untracked and ignored files | git 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
- Git Commit Reference - Commit conventions
- Git Branching Reference - Branch naming
- Git Tagging Reference - Versioning with tags
- First Contribution Tutorial - Complete workflow