基礎命令
# 生成SSH key
ssh-keygen -t rsa -C "your_email@youremail.com"
# 設置全局用戶名和郵箱, git 提交必須需要的
git config --global user.name "your name"
git config --global user.email "your_email@youremail.com"
#檢出倉庫
git clone git@github.com:yourName/yourRepo.git
# 添加遠程地址
git remote add origin git@github.com:yourName/yourRepo.git
# 創建倉庫
git init
git init newrepo
常用命令列表
查看狀態
git staus
添加到本地倉庫
現在,你的改動已經提交到了 HEAD,但是還沒到你的遠端倉庫
git add .
git commit -m "remark"
推送到遠程倉庫
git push origin master #不關聯本地址分支
git push -u origin master # 本地分支關聯遠程倉庫,第二次提交時只需使用: git push 即可
創建分支
#在當前分支上創建一個新分支並切換過去
git checkout -b test-dev
#切換回來
git checkout test
#刪除分支
git checkout -d test-dev
#重命令分支(先切換別的分支上再操作)
git branch -m test-dev test2
更新分支, 合並分支
git pull origin master # 未與遠程分支關聯
git pull # 如果分支使用了git push -u origin master
# 合並分支, 例A分支的代碼合並到B分支上, 先切換到B分支,然后操作
git checkout B && git merge A
#如果沖突的話,先處理分支,然后git add 提交
git add . && git commit -m "A merge To B branch"
查看分支的差異
git diff #尚未緩存的改動
git diff <被比較的分支,字體為紅色> <比較的分支,字體綠色>
git diff master dev # master字體為紅包, dev字體為綠色, - 代表減, + 代表添加
回滾
git checkout -- . #放棄未提交的所有文件
git reset HEAD
git log #查看提交的日志
git reflog #查看已回退的日志及所有的
git reset --hard log-id #回退到哪個版本ID
其它
#內建的圖形化 git:
gitk
#彩色輸出
git config color.ui true
#顯示歷史記錄時,每個提交的信息只顯示一行:
git config format.pretty oneline
縮寫命令
git config --global alias.st status
git config --global alias.co checkout
git config --global alias.ci commit
git config --global alias.br branch
git config --global alias.unstage 'reset HEAD'
git config --global alias.last 'log -1'
git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"