Git CLI

Branch

# List local branch only
git branch

# All branch
git branch -a

# Rename branch
git branch -m [old_branch] [new_branch]
git branch -m master main

# Delete branch
git branch -d <branch_name>

# Force delete branch
git branch -D <branch_name>

Clone

git clone [repo_url]

Config

# Init branch name as main
git config --global init.defaultBranch main

# Configure git user info
git config --global user.email "Your email"
git config --global user.name "Your Name"

# Get remote repo URL
git config --get remote.origin.url

# 查詢換行字元設定
git config --global core.autocrlf

# 禁止 Git 自動轉換
git config core.autocrlf false

Checkout

# Switch to branch
git checkout <branch_name>

# Create and switch branch
git checkout -b <branch_name>

Log

git log --oneline

git log --oneline --graph

git log --oneline --graph --all

Merge

# Merge [branch-name] into current branch
git merge <branch_name>

# Merge with no fast-forward
git merge --no-ff develop

Pull / Push

git pull

git pull origin main

# First time push with upstream
git push -u [Remote_Host] [Local_branch_name]:[Remote branch name]

Reset

# 退回指定 commit
git reset [hash_number]

git reset [hash_number]^

git reset [hash_number]~5

# Soft reset(保留變更)
git reset --soft HEAD~1

# Reset 單一檔案
git reset <filename>

Remote

# Show remote repos URL
git remote -v

# Set new remote URL
git remote set-url origin [new-url]

# Add remote
git remote add [name] <url>
git remote add origin <url>

Status

git status

Stash

# 恢復緩存的未提交檔案
git stash pop

Tag

# Create tag
git tag v0.1.0

# Push tag
git push origin v0.1.0

# Delete tag
git tag -d <old-tag-name>

# Create annotated tag
git tag -a <new-tag-name> -m "Tag message"

檢查檔案換行字元

git ls-files --eol

Use Cases

Rename a Remote Git Branch Name

git branch -a

git push origin --delete <old-name>

git push origin -u <new-name>

Single command:

git push origin :<old-name> <new-name>

Configure GitHub SSH

Create SSH key:

ssh-keygen -t ed25519 -C "your_email@example.com"

Add the SSH key to the ssh-agent:

eval "$(ssh-agent -s)"

Test connection:

ssh -T git@github.com

Or:

exec ssh-agent zsh

Add SSH private key to the ssh-agent:

ssh-add --apple-use-keychain ~/.ssh/id_ed25519_github

Show all agents:

ssh-add -l

SSH Agent

eval `ssh-agent -s`

ssh-add ~/.ssh/id_ed25519

cat ~/.ssh/id_ed25519.pub

Add GitHub to known hosts:

ssh-keyscan -H github.com >> ~/.ssh/known_hosts

Git Flow

init

git flow init

feature

# Create feature branch
git flow feature start my-feature

git add .
git commit -m "Add new feature"

# Merge into develop
git flow feature finish my-feature

bugfix

git flow bugfix start fix-bug

git add .
git commit -m "Fix bug"

# Merge into develop
git flow bugfix finish fix-bug

merge

# 合併 Feature 或 Bugfix 到 main 分支
git checkout main
git merge --no-ff develop
git push origin main

hotfix

git flow hotfix start fix-production-bug

git add .
git commit -m "Fix critical production bug"

git flow hotfix finish fix-production-bug

.gitignore

vi .gitignore
# 檔名
# 資料夾名稱/
# *.log

:wq  # 儲存檔案並離開

git add .
git commit -m "Added .gitignore"

Tree

tree -d -I 'target|test'