git常用命令

來源:互聯網
上載者:User

標籤:git   php   


1.設定使用者名稱/密碼
$ git config --global user.name "Your Name"$ git config --global user.email "[email protected]"

2 . 初始化一個Git倉庫,使用git init命令。

$ git initInitialized empty Git repository in /Users/michael/learngit/.git/


3.  添加檔案到Git倉庫,分兩步:

  • 第一步,使用命令git add <file>,注意,可反覆多次使用,添加多個檔案;

  • 第二步,使用命令git commit,完成。

$ git add readme.txt

$ git commit -m "wrote a readme file"[master (root-commit) cb926e7] wrote a readme file 1 file changed, 2 insertions(+) create mode 100644 readme.txt

4.
  • 要隨時掌握工作區的狀態,使用git status命令。

  • 如果git status告訴你有檔案被修改過,用git diff可以查看修改內容。

$ git status# On branch master# Changes not staged for commit:#   (use "git add <file>..." to update what will be committed)#   (use "git checkout -- <file>..." to discard changes in working directory)##    modified:   readme.txt#no changes added to commit (use "git add" and/or "git commit -a")

$ git diff readme.txt diff --git a/readme.txt b/readme.txtindex 46d49bf..9247db6 100644--- a/readme.txt+++ b/readme.txt@@ -1,2 +1,2 @@-Git is a version control system.+Git is a distributed version control system. Git is free software.

5
  • HEAD指向的版本就是目前的版本,因此,Git允許我們在版本的曆史之間穿梭,使用命令git reset --hard commit_id。

  • 穿梭前,用git log可以查看提交曆史,以便確定要回退到哪個版本。

  • 要重返未來,用git reflog查看命令曆史,以便確定要回到未來的哪個版本。

      git log 查看曆史提交

$ git logcommit 3628164fb26d48395383f8f31179f24e0882e1e0Author: Michael Liao <[email protected]>Date:   Tue Aug 20 15:11:49 2013 +0800    append GPLcommit ea34578d5496d7dd233c827ed32a8cd576c5ee85Author: Michael Liao <[email protected]>Date:   Tue Aug 20 14:53:12 2013 +0800?    add distributedcommit cb926e7ea50ad11b8f9e909c05226233bf755030Author: Michael Liao <[email protected]>Date:   Mon Aug 19 17:51:55 2013 +0800?    wrote a readme file

$ git log --pretty=oneline3628164fb26d48395383f8f31179f24e0882e1e0 append GPLea34578d5496d7dd233c827ed32a8cd576c5ee85 add distributedcb926e7ea50ad11b8f9e909c05226233bf755030 wrote a readme file

$ cat readme.txtGit is a distributed version control system.Git is free software.
$ git reset --hard 3628164HEAD is now at 3628164 append GPL

$ git reflogea34578 [email protected]{0}: reset: moving to HEAD^3628164 [email protected]{1}: commit: append GPLea34578 [email protected]{2}: commit: add distributedcb926e7 [email protected]{3}: commit (initial): wrote a readme file

6.

情境1:當你改亂了工作區某個檔案的內容,想直接丟棄工作區的修改時,用命令git checkout -- file。

情境2:當你不但改亂了工作區某個檔案的內容,還添加到了暫存區時,想丟棄修改,分兩步,第一步用命令git reset HEAD file,就回到了情境1,第二步按情境1操作。

git checkout -- file可以丟棄工作區的修改:

$ git checkout -- readme.txt
用命令git reset HEAD file可以把暫存區的修改撤銷掉(unstage),重新放回工作區:
$ git reset HEAD readme.txtUnstaged changes after reset:M       readme.txt

7.要從版本庫中刪除該檔案,那就用命令git rm刪掉,並且git commit:

$ git rm test.txtrm 'test.txt'$ git commit -m "remove test.txt"[master d17efd8] remove test.txt 1 file changed, 1 deletion(-) delete mode 100644 test.txt

git checkout其實是用版本庫裡的版本替換工作區的版本,無論工作區是修改還是刪除,都可以“一鍵還原”。

$ git checkout -- test.txt
8.添加遠程倉庫

git remote add origin [email protected]:michaelliao/learngit.git

把本地庫的所有內容推送到遠程庫上:
$ git push -u origin masterCounting objects: 19, done.Delta compression using up to 4 threads.Compressing objects: 100% (19/19), done.Writing objects: 100% (19/19), 13.73 KiB, done.Total 23 (delta 6), reused 0 (delta 0)To [email protected]:michaelliao/learngit.git * [new branch]      master -> masterBranch master set up to track remote branch master from origin.
只要本地作了提交,就可以通過命令:
$ git push origin master

要關聯一個遠程庫,使用命令git remote add origin [email protected]:path/repo-name.git;

關聯後,使用命令git push -u origin master第一次推送master分支的所有內容;

此後,每次本地提交後,只要有必要,就可以使用命令git push origin master推送最新修改;

9.

要複製一個倉庫,首先必須知道倉庫的地址,然後使用git clone命令複製。

Git支援多種協議,包括https,但通過ssh支援的原生git協議速度最快。

$ git clone [email protected]:michaelliao/gitskills.gitCloning into 'gitskills'...remote: Counting objects: 3, done.remote: Total 3 (delta 0), reused 0 (delta 0)Receiving objects: 100% (3/3), done.

10。

查看分支:git branch

建立分支:git branch <name>

切換分支:git checkout <name>

建立+切換分支:git checkout -b <name>

合并某分支到當前分支:git merge <name>

刪除分支:git branch -d <name>

$ git checkout -b devSwitched to a new branch 'dev'

$ git branch dev$ git checkout devSwitched to branch 'dev'
$ git branch* dev  master


$ git merge devUpdating d17efd8..fec145aFast-forward readme.txt |    1 + 1 file changed, 1 insertion(+)
$ git branch -d devDeleted branch dev (was fec145a).

11.用git log --graph命令可以看到分支合并圖

$ git log --graph --pretty=oneline --abbrev-commit*   59bc1cb conflict fixed|| * 75a857c AND simple* | 400b400 & simple|/* fec145a branch test...

12.合并分支時,加上--no-ff參數就可以用普通模式合并,合并後的曆史有分支,能看出來曾經做過合并,而fast forward合并就看不出來曾經做過合并。

$ git merge --no-ff -m "merge with no-ff" devMerge made by the 'recursive' strategy. readme.txt |    1 + 1 file changed, 1 insertion(+)

13.

修複bug時,我們會通過建立新的bug分支進行修複,然後合并,最後刪除;

當手頭工作沒有完成時,先把工作現場git stash一下,然後去修複bug,修複後,再git stash pop,回到工作現場。

stash功能,可以把當前工作現場“儲藏”起來,等以後恢複現場後繼續工作:
$ git stashSaved working directory and index state WIP on dev: 6224937 add mergeHEAD is now at 6224937 add merge<pre name="code" class="plain">$ git stash list[email protected]{0}: WIP on dev: 6224937 add merge

$ git stash pop# On branch dev# Changes to be committed:#   (use "git reset HEAD <file>..." to unstage)##       new file:   hello.py## Changes not staged for commit:#   (use "git add <file>..." to update what will be committed)#   (use "git checkout -- <file>..." to discard changes in working directory)##       modified:   readme.txt#Dropped refs/[email protected]{0} (f624f8e5f082f2df2bed8a4e09c12fd2943bdd40)

$ git stash apply [email protected]{0}



14
  • 查看遠程庫資訊,使用git remote -v;

  • 本地建立的分支如果不推送到遠程,對其他人就是不可見的;

  • 從本地推送分支,使用git push origin branch-name,如果推送失敗,先用git pull抓取遠端新提交;

  • 在本地建立和遠程分支對應的分支,使用git checkout -b branch-name origin/branch-name,本地和遠程分支的名稱最好一致;

  • 建立本地分支和遠程分支的關聯,使用git branch --set-upstream branch-name origin/branch-name;

  • 從遠程抓取分支,使用git pull,如果有衝突,要先處理衝突。

$ git remote -vorigin  [email protected]:michaelliao/learngit.git (fetch)origin  [email protected]:michaelliao/learngit.git (push)
$ git push origin master
$ git branch --set-upstream dev origin/devBranch dev set up to track remote branch dev from origin.
15.標籤
  • 命令git tag <name>用於建立一個標籤,預設為HEAD,也可以指定一個commit id;

  • git tag -a <tagname> -m "blablabla..."可以指定標籤資訊;

  • git tag -s <tagname> -m "blablabla..."可以用PGP簽名標籤;

  • 命令git tag可以查看所有標籤。

  • 命令git push origin <tagname>可以推送一個本地標籤;

  • 命令git push origin --tags可以推送全部未推送過的本地標籤;

  • 命令git tag -d <tagname>可以刪除一個本地標籤;

  • 命令git push origin :refs/tags/<tagname>可以刪除一個遠程標籤。



 

git常用命令

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.