Git 常用命令速查表
創(chuàng)建版本庫(kù)
-
$ git clone <url>
#克隆遠(yuǎn)程版本庫(kù) -
$ git init
#初始化本地版本庫(kù)
修改和提交
-
$ git status
#查看狀態(tài) -
$ git diff
#查看變更內(nèi)容 -
$ git add .
#跟蹤所有改動(dòng)過的文件 -
$ git add <file>
#跟蹤指定的文件 -
$ git mv <old> <new>
#文件改名 -
$ git rm <file>
#刪除文件 -
$ git rm --cached <file>
#停止跟蹤文件但不刪除 -
$ git commit -m “commit message”
#提交所有更新過的文件 -
$ git commit --amend
#修改最后一次提交
查看提交歷史
$ git log
#查看提交歷史$ git log -p <file>
#查看指定文件的提交歷史$ git blame <file>
#以列表方式查看指定文件的提交歷史
撤消
-
$ git reset --hard HEAD
#撤消工作目錄中所有未提交文件的修改內(nèi)容 -
$ git checkout HEAD <file>
#撤消指定的未提交文件的修改內(nèi)容 -
$ git revert <commit>
#撤消指定的提交
分支與標(biāo)簽
-
$ git branch
#顯示所有本地分支 -
$ git checkout <branch/tag>
#切換到指定分支或標(biāo)簽 -
$ git branch <new-branch>
#創(chuàng)建新分支 -
$ git branch -d <branch>
#刪除本地分支 -
$ git tag
#列出所有本地標(biāo)簽 -
$ git tag <tagname>
#基于最新提交創(chuàng)建標(biāo)簽 -
$ git tag -d <tagname>
#刪除標(biāo)簽
合并與衍合
-
$ git merge <branch>
#合并指定分支到當(dāng)前分支 -
$ git rebase <branch>
#衍合指定分支到當(dāng)前分支
遠(yuǎn)程操作
-
$ git remote -v
#查看遠(yuǎn)程版本庫(kù)信息 -
$ git remote show <remote>
#查看指定遠(yuǎn)程版本庫(kù)信息 -
$ git remote add <remote> <url>
#添加遠(yuǎn)程版本庫(kù) -
$ git fetch <remote>
#從遠(yuǎn)程庫(kù)獲取代碼 -
$ git pull <remote> <branch>
#下載代碼及快速合并 -
$ git push <remote> <branch>
#上傳代碼及快速合并 -
$ git push <remote> :<branch/tag-name>
#刪除遠(yuǎn)程分支或標(biāo)簽 -
$ git push --tags
#上傳所有標(biāo)簽
復(fù)制版本庫(kù)
如果你不想派生項(xiàng)目划乖,而只是想復(fù)制一份相同的源代碼,或者想從別的 Git 托管服務(wù)那里復(fù)制一份源代碼到 GitCafe 上的話,可以通過以下步驟來操作铸董。
1). 從原地址克隆一份裸版本庫(kù)养交,當(dāng)然你也可以把托管于其它 git 服務(wù)器上的版本庫(kù)克隆下來伺糠。
git clone --bare git://gitcafe.com/username/project.git
2). 然后到 GitCafe 服務(wù)器上創(chuàng)建一個(gè)新項(xiàng)目伦泥。
3). 以鏡像推送的方式上傳代碼到 GitCafe 服務(wù)器上蹬耘。
cd project.git
git push --mirror git@gitcafe.com/username/newproject.git
4). 刪除本地代碼
cd ..
rm -rf project.git
派生項(xiàng)目與上游代碼庫(kù)保持同步
1). 在 Fork 的代碼庫(kù)中添加上游代碼庫(kù)的 remote 源芝雪,(操作一次就可以,以后不必每次添加)
git remote add upstream git://gitcafe.com/username/upstream
# upstream 表示上游代碼庫(kù)名稱
2). 本地修改和提交 (commit)
3). 在每次 Pull Request 前做如下操作综苔,即可實(shí)現(xiàn)和上游版本庫(kù)的同步惩系。
git remote update upstream
git rebase upstream/master
# 如果不是 master 分支,請(qǐng)把 master 改為相應(yīng)的分支名如筛,
同時(shí)在 rebase 前用 git checkout 命令切換到相應(yīng)的本地分支
4). Push 代碼到 GitCafe
git push
參考:ProGit-分支的衍合
HTTP Errors
如果你在使用 HTTP 協(xié)議進(jìn)行 Git 操作的時(shí)候出現(xiàn)錯(cuò)誤提示:
-
401 錯(cuò)誤:
$ git push origin master
error: RPC failed; result=22, HTTP code = 401
fatal: The remote end hung up unexpectedly
fatal: The remote end hung up unexpectedly
Everything up-to-date -
403 錯(cuò)誤:
$ git push origin master
error: RPC failed; result=22, HTTP code = 401
fatal: The remote end hung up unexpectedly
fatal: The remote end hung up unexpectedly
Everything up-to-date
有以下幾個(gè)可能性:
-
Git 版本過低堡牡。GitCafe 推薦使用的 Git 版本是 >= 1.7,請(qǐng)參考這里獲取最新版本杨刨。
$ git --version
git version 1.8.2.1 -
遠(yuǎn)程倉(cāng)庫(kù)路徑設(shè)置錯(cuò)誤晤柄。注意,GitCafe 對(duì)于路徑的識(shí)別是大小寫敏感的拭嫁。
查看已有的遠(yuǎn)程倉(cāng)庫(kù):
$ git remote -v origin https://gitcafe.com/GitCafe/help.git (fetch) origin https://gitcafe.com/GitCafe/help.git (push)
設(shè)置新的遠(yuǎn)程倉(cāng)庫(kù)路徑:
$ git remote set-url origin https://gitcafe.com/GitCafe/Help.git
查看新的遠(yuǎn)程倉(cāng)庫(kù)路徑:
$ git remote -v origin https://gitcafe.com/GitCafe/Help.git (fetch) origin https://gitcafe.com/GitCafe/Help.git (push)
對(duì)該倉(cāng)庫(kù)沒有訪問權(quán)限可免。檢查你是否對(duì)目標(biāo)倉(cāng)庫(kù)有相應(yīng)的讀寫權(quán)限。
輸入了錯(cuò)誤的用戶名和密碼做粤。檢查你是否使用了對(duì)該倉(cāng)庫(kù)有寫權(quán)限的正確的賬戶名稱和密碼浇借,檢查是否對(duì)所有你名下的倉(cāng)庫(kù)均不能訪問。
Git 相關(guān)問題
Git 介紹 (Git.md)
Git 常用命令速查表 (Git_Cheat_Sheet.md)
Git 常用命令速查表PDF版 (Git_Cheat_Sheet.pdf)
Git 常用命令速查表PNG版 (Git_Cheat_Sheet.png)
派生項(xiàng)目與上游代碼庫(kù)保持同步 (Sync_With_Upstream.md)
復(fù)制版本庫(kù) (Duplicating_Repo.md)
HTTP錯(cuò)誤列表 (HTTP_Errors.md)
Git Community Book 中文版 (Git_Community_Book.pdf)
Pro Git 中文版 (Pro_Git.pdf)
來自GitCafe