-
Setup
# Initial global setup on system
git config --global user.name "Your Name"
git config --global user.email "your@email.com"
git config --global color.ui true
git config --global core.autocrlf true
-
Create
## From data/local directory
# Initialize git
git init
# Add remote url
git remote add origin https://HOST.COM/USERNAME/REPOSITORY.git
# Add all files
git add .
# Commit all
git commit -m "Initial commit"
# Push to remote
git push -u origin BRANCH
## From repo/remote url
# Clone repository
git clone https://HOST.COM/USERNAME/REPOSITORY.git
# Create/Add files into the cloned directory
# Add all files
git add .
# Commit all
git commit -m "Initial commit"
# Push to remote
git push -u origin BARNCH
-
Browse
# View changed files
git status
# View History
git log
# View modified files
git log --stat
# View commits in one line format
git log --oneline
# Who made changes
git blame FILE
# View changes
git diff
# View changes between commits
git diff ID1 ID2
# View changes between branches
git diff BRANCH1 BRANCH2
# View changed files between branches
git diff --name-status BRANCH1 BRANCH2
-
Update
# Fetch
git fetch BRANCH
# Fetch and merge
git pull BRANCH
# Push changes to remote
git push origin BRANCH
# Force push changes to remote
git push -f origin BRANCH
-
Branch
# View branches
git branch
# Create branch
git checkout -b BRANCH
# Switch between branches
git checkout BRANCH
# Merge a branch into the active branch
git merge BRANCH2
# Delete local branch
git branch -d BRANCH
# Delete remote branch
git push origin --delete BRANCH
-
Local work
# Stage files
git add FILE
# Stage directory
git add DIRECTORY
# Stage all
git add .
# Commit files
git commit FILE -m "commit message"
# Commit all tracked files
git commit -a -m "commit message"
# Edit last commit message
git commit --amend
-
Reset
# Unstage file
git reset -- FILE
# Unstage all
git reset
# Untrack file
git rm --cached FILE
# Untrack directory
git rm -r --cached DIRECTORY
# Revert to last commit
git reset --hard HEAD^
# Revert to specific commit
git reset --hard COMMIT_ID
# Reset file
git checkout -- FILE
-
Stash
# Save a stash
git stash
# View stashes
git stash list
# Apply stash
git stash apply NUMBER
# Apply and remove
git stash pop NUMBER
# Clear all stashes
git stash clear
-
Tag
# List all tags
git tag
# Tag a commit
git tag -a 1.2 -m "stable version"
# Push tag to remote
git push origin TAGNAME
# Delete local tag
git tag --delete TAGNAME
# Delete remote tag
git push --delete origin TAGNAME