Git命令

jopen 11年前發布 | 29K 次閱讀 Git 版本控制系統

符號約定:

[]:可選  <>:必選

Git 配置

git config [--global] user.name <name>        設置用戶名

git config [--global] user.email <email>         設置郵箱

git config [--global] core.editor <editor>        設置編輯器

git config [--global] github.user <user>         設置github帳號名

git config [--global] github.token <token>        設置github的token

--global是對當前系統用戶的全局設置,在~/.gitconfig中。對系統所有用戶進行配置,/etc/gitconfig。對當前項目,.git/config

Git 創建庫

git clone <url>                   ssh/http(s)/git三種協議,ssh和https可推送

git init                        初始化Git倉庫

Git 日常操作

git add <file>                 將文件加入index file

git rm [--cached]                刪除,加--cached表示僅從index file中刪除文件,即放棄跟蹤

git mv <src> <dest>             移動/更名

git diff --cached/--staged          當前索引與上次提交(有哪些需要commit)

git diff                      當前索引與工作目錄(有哪些需要add)

git diff HEAD[^]               工作目錄與上次提交(當前目錄與上次提交有何改變)

git commit [-a] -m <msg>          提交

git commit --amend [-m <msg>]       修復上次提交

git reset HEAD <file>             同--mixed,default option

git reset --mixed HEAD            撤銷 commit 和index file,只保留 working tree 的信息

git reset --hard HEAD[^]           將 working tree 和 index file 都撤銷到以前狀態

git reset --soft HEAD[^]            只撤銷 commit,而保留 working tree 和 index file 的信息

                     回復到某個狀態。以git reset --soft HEAD為例,commit回退到

                     HEAD(相當于無變化),若是HEAD^,則commit回退到HEAD^

git gc                     用垃圾回收機制清除由于 reset 而造成的垃圾代碼

git status                  顯示當前工作目錄狀態

git log [-p]                   顯示提交歷史(many useful options to be learned)

git branch [branch]               顯示/新建分支

git branch -d/-D               刪除分支(d表示“在分支合并后刪除分支”,D表示無論如何都刪除分支)

git show-branch

git checkout <branch>            切換分支(分支未commit無法切換)

git merge <branch>              合并分支

git merge == git pull .

git show <branch | commit | tag | etc>        顯示對應對象的信息

git grep <rep> [object]             (在指定對象(歷史記錄)中)搜索        

git cat-file                    查看數據

git cat-file <-t | -s | -e | -p | (type)> <object>        type can be one of: blob, tree, commit, tag

git ls-files [--stage]              show information about files in the index and the working tree(實際是查看索引文件)

git watchchanged <since>..<until>       顯示兩個commit(當然也可以是branch)的區別

git remote [-v]                    顯示遠程倉庫,加-v選項可顯示倉庫地址

git remote add <repo_name> <url>         添加遠程倉庫,repo_name為shortname,指代倉庫地址

git remote rename <old_name> <new_name>    更名

git remote rm <repo_name>            刪除遠程倉庫

git remote show <repo_name>          查看遠程倉庫信息

git remote fetch <repo_name>           從遠程倉庫抓取數據(并不合并)

git pull <repo_name> <branch_name>      拉去數據并合并到當前分支

git push <repo_name> <branch_name>      推送指定分支到指定倉庫

git fetch <repo_name> <branch_name>[:<local_branch_name>]    拉去數據,未合并

Git 標簽

git 標簽相關……

Git 相關環境變量

GIT_DIR: 如果指定了那么git init將會在GIT_DIR指定的目錄下創建版本庫

GIT_OBJECT_DIRECTORY: 用來指示對象存儲目錄的路徑。即原來$GIT_DIR/objects下的文件會置于該變量指定的路徑下

Git 常見變量

HEAD: 表示最近一次的 commit。

MERGE_HEAD: 如果是 merge 產生的 commit,那么它表示除 HEAD 之外的另一個父母分支。

FETCH_HEAD: 使用 git-fetch 獲得的 object 和 ref 的信息都存儲在這里,這些信息是為日后 git-merge 準備的。

HEAD^: 表示 HEAD 父母的信息

HEAD^^: 表示 HEAD 父母的父母的信息

HEAD~4: 表示 HEAD 上溯四代的信息

HEAD^1: 表示 HEAD 的第一個父母的信息

HEAD^2: 表示 HEAD 的第二個父母的信息

COMMIT_EDITMSG: 最后一次 commit 時的提交信息。

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!