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 falseCheckout
# 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 --allMerge
# Merge [branch-name] into current branch
git merge <branch_name>
# Merge with no fast-forward
git merge --no-ff developPull / 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 statusStash
# 恢復緩存的未提交檔案
git stash popTag
# 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 --eolUse 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.comOr:
exec ssh-agent zshAdd SSH private key to the ssh-agent:
ssh-add --apple-use-keychain ~/.ssh/id_ed25519_githubShow all agents:
ssh-add -lSSH Agent
eval `ssh-agent -s`
ssh-add ~/.ssh/id_ed25519
cat ~/.ssh/id_ed25519.pubAdd GitHub to known hosts:
ssh-keyscan -H github.com >> ~/.ssh/known_hostsGit Flow
init
git flow initfeature
# 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-featurebugfix
git flow bugfix start fix-bug
git add .
git commit -m "Fix bug"
# Merge into develop
git flow bugfix finish fix-bugmerge
# 合併 Feature 或 Bugfix 到 main 分支
git checkout main
git merge --no-ff develop
git push origin mainhotfix
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'