Git 常用命令速查表
創建版本庫
-
$ git clone <url>
#克隆遠程版本庫 -
$ git init
#初始化本地版本庫
修改和提交
-
$ git status
#查看狀態 -
$ git diff
#查看變更內容 -
$ git add .
#跟蹤所有改動過的文件 -
$ 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
#撤消工作目錄中所有未提交文件的修改內容 -
$ git checkout HEAD <file>
#撤消指定的未提交文件的修改內容 -
$ git revert <commit>
#撤消指定的提交
分支與標簽
-
$ git branch
#顯示所有本地分支 -
$ git checkout <branch/tag>
#切換到指定分支或標簽 -
$ git branch <new-branch>
#創建新分支 -
$ git branch -d <branch>
#刪除本地分支 -
$ git tag
#列出所有本地標簽 -
$ git tag <tagname>
#基于最新提交創建標簽 -
$ git tag -d <tagname>
#刪除標簽
合并與衍合
-
$ git merge <branch>
#合并指定分支到當前分支 -
$ git rebase <branch>
#衍合指定分支到當前分支
遠程操作
-
$ git remote -v
#查看遠程版本庫信息 -
$ git remote show <remote>
#查看指定遠程版本庫信息 -
$ git remote add <remote> <url>
#添加遠程版本庫 -
$ git fetch <remote>
#從遠程庫獲取代碼 -
$ git pull <remote> <branch>
#下載代碼及快速合并 -
$ git push <remote> <branch>
#上傳代碼及快速合并 -
$ git push <remote> :<branch/tag-name>
#刪除遠程分支或標簽 -
$ git push --tags
#上傳所有標簽
復制版本庫
如果你不想派生項目,而只是想復制一份相同的源代碼,或者想從別的 Git 托管服務那里復制一份源代碼到 GitCafe 上的話,可以通過以下步驟來操作。
1). 從原地址克隆一份裸版本庫,當然你也可以把托管于其它 git 服務器上的版本庫克隆下來。
git clone --bare git://gitcafe.com/username/project.git
2). 然后到 GitCafe 服務器上創建一個新項目。
3). 以鏡像推送的方式上傳代碼到 GitCafe 服務器上。
cd project.git
git push --mirror git@gitcafe.com/username/newproject.git
4). 刪除本地代碼
cd ..
rm -rf project.git
派生項目與上游代碼庫保持同步
1). 在 Fork 的代碼庫中添加上游代碼庫的 remote 源,(操作一次就可以,以后不必每次添加)
git remote add upstream git://gitcafe.com/username/upstream
# upstream 表示上游代碼庫名稱
2). 本地修改和提交 (commit)
3). 在每次 Pull Request 前做如下操作,即可實現和上游版本庫的同步。
git remote update upstream
git rebase upstream/master
# 如果不是 master 分支,請把 master 改為相應的分支名,
同時在 rebase 前用 git checkout 命令切換到相應的本地分支
4). Push 代碼到 GitCafe
git push
參考:ProGit-分支的衍合
HTTP Errors
如果你在使用 HTTP 協議進行 Git 操作的時候出現錯誤提示:
-
401 錯誤:
$ 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 錯誤:
$ 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
有以下幾個可能性:
-
Git 版本過低。GitCafe 推薦使用的 Git 版本是 >= 1.7,請參考這里獲取最新版本。
$ git --version
git version 1.8.2.1 -
遠程倉庫路徑設置錯誤。注意,GitCafe 對于路徑的識別是大小寫敏感的。
查看已有的遠程倉庫:
$ git remote -v origin https://gitcafe.com/GitCafe/help.git (fetch) origin https://gitcafe.com/GitCafe/help.git (push)
設置新的遠程倉庫路徑:
$ git remote set-url origin https://gitcafe.com/GitCafe/Help.git
查看新的遠程倉庫路徑:
$ git remote -v origin https://gitcafe.com/GitCafe/Help.git (fetch) origin https://gitcafe.com/GitCafe/Help.git (push)
對該倉庫沒有訪問權限。檢查你是否對目標倉庫有相應的讀寫權限。
輸入了錯誤的用戶名和密碼。檢查你是否使用了對該倉庫有寫權限的正確的賬戶名稱和密碼,檢查是否對所有你名下的倉庫均不能訪問。
Git 相關問題
Git 介紹 (Git.md)
Git 常用命令速查表 (Git_Cheat_Sheet.md)
Git 常用命令速查表PDF版 (Git_Cheat_Sheet.pdf)
Git 常用命令速查表PNG版 (Git_Cheat_Sheet.png)
派生項目與上游代碼庫保持同步 (Sync_With_Upstream.md)
復制版本庫 (Duplicating_Repo.md)
HTTP錯誤列表 (HTTP_Errors.md)
Git Community Book 中文版 (Git_Community_Book.pdf)
Pro Git 中文版 (Pro_Git.pdf)
來自GitCafe