Table of Contents
- Initialize a New Repository
- Clone a Repository
- Clone a Specific Branch
- Clone Into a Specific Directory
- Repository Information
- Staging Changes
- Commits
- Branches
- Remote Branches
- Remotes
- Pulling and Fetching
- Merging
- Rebasing
- Comparing Changes
- Viewing Files
- Stashing
- Undo Changes
- Tags
- Working with Bare Repositories
- Exporting Files
- Cleaning
- Troubleshooting
- Common Workflows
Initialize a New Repository
git init
Clone a Repository
git clone user@server:/srv/repo
Clone a Specific Branch
git clone --single-branch -b customer-map user@server:/srv/repo
Clone Into a Specific Directory
git clone user@server:/srv/repo my-project
Repository Information
Show Repository Status
git status
Show Commit History
git log
Compact Commit History
git log --oneline
Graph View
git log --oneline --graph --all --decorate
Show Current Branch
git branch --show-current
Show Remotes
git remote -v
Staging Changes
Stage a File
git add file.txt
Stage All Changes
git add .
Unstage a File
git restore --staged file.txt
Stage Deleted Files
git add -u
Commits
Commit Changes
git commit -m "Added feature"
Commit All Tracked Changes
git commit -am "Updated files"
Amend Last Commit
git commit --amend
Amend Without Changing Message
git commit --amend --no-edit
Branches
List Local Branches
git branch
List Remote Branches
git branch -r
List All Branches
git branch -a
Create Branch
git branch feature-x
Create and Switch Branch
git checkout -b feature-x
or
git switch -c feature-x
Switch Branch
git checkout main
or
git switch main
Rename Current Branch
git branch -m new-name
Delete Branch
git branch -d feature-x
Force Delete Branch
git branch -D feature-x
Remote Branches
Push New Branch
git push -u origin feature-x
Push Existing Branch
git push
Fetch Remote Branches
git fetch
Create Local Branch from Remote
git checkout -b feature-x origin/feature-x
Track Existing Remote Branch
git branch --set-upstream-to=origin/feature-x
Delete Remote Branch
git push origin --relete feature-x
Remotes
Add Remote
git remote add origin user@server:/srv/repo
Change Remote URL
git remote set-url origin user@server:/srv/repo
Remove Remote
git remote remove origin
Rename Remote
git remote rename origin production
Show Remote Details
git remote show origin
Pulling and Fetching
Fetch Updates
git fetch
Pull Changes
git pull
Pull Specific Branch
git pull origin main
Pull Unrelated Histories
git pull origin main --allow-unrelated-histories
Merging
Merge Branch into Current Branch
git merge feature-x
Merge Without Fast Forward
git merge --no-ff feature-x
Abort Merge
git merge --abort
Rebasing
Rebase Current Branch onto Main
git rebase main
Rebase Onto Remote Main
git fetch
git rebase origin/main
Abort Rebase
git rebase --abort
Continue Rebase
git rebase --continue
Comparing Changes
Compare Working Tree to HEAD
git diff
Compare Staged Changes
git diff --cached
Compare Branches
git diff main..feature-x
Show Changed Files Between Branches
git diff --name-status main..feature-x
Compare Remote Branches
git diff origin/main..origin/feature-x
Viewing Files
List Files in Commit
git ls-tree -r HEAD --name-only
Show File History
git log -- file.txt
Show File Contents from Branch
git show main:file.txt
Stashing
Save Changes
git stash
Save with Description
git stash push -m "Work in progress"
List Stashes
git stash list
Restore Most Recent Stash
git stash pop
Apply Without Removing
git stash apply
Delete Stash
git stash drop stash@{0}
Undo Changes
Restore File
git restore file.txt
Restore Entire Working Tree
git restore .
Reset File to HEAD
git checkout HEAD -- file.txt
Soft Reset
git reset --soft HEAD~1
Mixed Reset
git reset HEAD~1
Hard Reset
git reset --hard HEAD~1
Reset to Remote
git fetch
git reset --hard origin/main
Tags
Create Tag
git tag v1.0
Annotated Tag
git tag -a v1.0 -m "Release 1.0"
Push Tag
git push origin v1.0
Push All Tags
git push --tags
Delete Local Tag
git tag -d v1.0
Delete Remote Tag
git push origin :refs/tags/v1.0
Working with Bare Repositories
Create Bare Repository
git init --bare /srv/repo
Clone Bare Repository
git clone user@server:/srv/repo
Show References
git show-ref
Exporting Files
Archive Branch
git archive --format=tar.gz HEAD -o project.tar.gz
Export Branch to Directory
git archive main | tar -x -C /tmp/export
Cleaning
Show Files to be Removed
git clean -nd
Remove Untracked Files
git clean -fd
Remove Untracked Files and Directories
git clean -fdx
Troubleshooting
Detect Current Upstream
git rev-parse --abbrev-ref --symbolic-full-name @{u}
Show Tracking Branches
git branch -vv
Find Unmerged Branches
git branch --no-merged
Find Merged Branches
git branch --merged
Verify Repository
git fsck
Show Configuration
git config --list
Show Origin URL
git config --get remote.origin.url
Show Global Username
git config --global user.name
Show Global Email
git config --global user.email
Common Workflows
Create Feature Branch
git checkout main
git pull
git checkout -b feature-x
Push Feature Branch
git add .
git commit -m "Added feature"
git push -u origin feature-x
Merge Feature Branch into Main
git checkout main
git pull
git merge feature-x
git push
Update Feature Branch from Main
git checkout feature-x
git fetch origin
git merge origin/main
Replace Remote Branch with Another Branch
git checkout tfind-staging
git push origin tfind-staging:tfind --force
Create Local Branch from Remote
git fetch origin
git checkout -b customer-map origin/customer-map
Synchronize Local Branch with Remote
git fetch origin
git reset --hard origin/main