1、創(chuàng)建倉(cāng)庫(kù)
git clone
$ git clone <url> 克隆遠(yuǎn)程倉(cāng)庫(kù)到本地
git init
$ git init 初始化本地倉(cāng)庫(kù)
2、修改和提交
git status
$ git status 查看狀態(tài)
git add
$ git add . 跟蹤所有改動(dòng)過(guò)的文件
$ git add <file> 跟蹤指定的文件
git mv
$ git mv <old> <new> 修改文件名
git commit
$ git commit -m "commit messages..." 提交所有更新過(guò)的文件,并記錄一行簡(jiǎn)單提交信息
$ git commit 啟動(dòng)編輯器,記錄詳細(xì)提交信息
$ git commit --amend 修改最后一次提交信息
$ git commit -am "Some information..." 添加跟蹤并提交文件
3、查看提交歷史
git log
$ git log 查看提交歷史
$ git log <file> 只顯示指定目錄、文件的日志
$ git log -p <file> 顯示文件的改動(dòng),只查看文件的提交日志以及提交前后的差別
$ git blame <file> 以列表方式查看指定文件的提交歷史
$ git log --pretty-short 只顯示提交信息的第一行
$ git log --graph 以圖表形式查看分支
git diff
$ git diff 查看工作樹(shù)和暫存區(qū)的差別
$ git diff HEAD 查看工作樹(shù)和最新提交的差別
git reflog
$ git reflog 查看當(dāng)前倉(cāng)庫(kù)的日志
4、撤銷
git reset
$ git reset --hard <commit> 回溯歷史版本
$ git reset --hard HEAD 撤銷工作目錄中所有未提交文件的修改內(nèi)容
$ git checkout HEAD <file> 撤銷指定的未提交文件的修改內(nèi)容
<commit>即某一次commit的hashcode。
關(guān)于git reset命令的詳細(xì)介紹,請(qǐng)參考另一篇文章:git reset 命令詳解。
git revert
$ git revert <commit> 撤銷指定的提交
git log
$ git log --before="1 days" 退回到之前1天的版本
git rm
$ git rm -f <file> 強(qiáng)制刪除文件
$ git rm --cached <file> 停止跟蹤文件,但不刪除
5、分支與標(biāo)簽
git branch
$ git branch 顯示所有本地分支
$ git branch -a 同時(shí)顯示本地倉(cāng)庫(kù)和遠(yuǎn)程倉(cāng)庫(kù)的分支信息
$ git branch <new-branch> 創(chuàng)建新分支
$ git branch -d <branch> 刪除本地分支
git checkout
$ git checkout <branch> 切換到指定分支
$ git checkout -b branch-A 創(chuàng)建分支A,并切換到分支A
6、合并
git merge
$ git merge <branch> 合并指定分支到當(dāng)前分支
$ git merge --no-ff branch-A 將分支A合并到master分支。
7、遠(yuǎn)程操作
git remote
$ git remote -v 查看遠(yuǎn)程倉(cāng)庫(kù)庫(kù)信息
$ git remote show <remote> 查看指定遠(yuǎn)程倉(cāng)庫(kù)信息
$ git remote add <remote> <url> 設(shè)置本地倉(cāng)庫(kù)的遠(yuǎn)程倉(cāng)庫(kù)
$ git remote rename <remote> <remote new name> 重命名遠(yuǎn)程倉(cāng)庫(kù)
$ git remote rm <remote> 解除遠(yuǎn)程倉(cāng)庫(kù)關(guān)聯(lián)
<remote>默認(rèn)為origin。
git fetch
$ git fetch <remote> 從遠(yuǎn)程倉(cāng)庫(kù)獲取代碼
git pull
$ git pull <remote> <branch> 下載代碼及快速合并
git push
$ git push 上傳當(dāng)前分支代碼到遠(yuǎn)程倉(cāng)庫(kù)
$ git push -u origin <branch> 在遠(yuǎn)程倉(cāng)庫(kù)新建分支,并上傳代碼
8、其他
git config
$ git config --global user.name "Your Name"
$ git config --global user.email "e-mail@example.com"
$ git config --list 查看用戶配置列表
git --version
$ git --version 可以查看當(dāng)前 Git 版本
touch
$ touch .gitignore 創(chuàng)建.gitignore文件