git常用命令總結

來源:互聯網
上載者:User

標籤:

一、定義:以下均使用括弧中的簡稱

工作區:workspace (W)

暫存區:index (I)

本地倉庫:local repository (L)

遠程倉庫:remote repository (R)

二、常用命令:

1.初始化

git init

2.常用配置資訊 

(如果不使用--global,則只設定當前項目的配置資訊)

設定代理:

git config --global http.proxy http://xxx.com:port

設定使用者名稱:

git config --global user.name "[name]"  

設定郵箱: 

git config --global user.email "[email address]" 

設定命令列顏色:

git config --global color.ui auto

查看使用者名稱:

git config --global user.name

配置crlf:

git config --global core.autocrlf [true/false/input]

(在windows下設定為true,則提交的內容是LF,本地是CRLF)

3.添加--從工作區W到暫存區I

添加單個檔案/多個檔案/目錄:

git add [file1] [file2] [directory] 

添加新增和修改的檔案,不做刪除操作:

git add . 

添加修改和刪除的檔案,不做新增操作:

git add -u

添加增刪改的檔案:等於git add .;git add -u;  

git add -A

4.提交:

(1)從暫存區I到本地倉庫L

git commit -m [提交資訊]

(2)

5.撤銷

(1)從暫存區I到工作區W

git rm --cached <file> ...

(2)恢複到HEAD版本

git reset --hard HEAD

(3)修改提交的使用者名稱資訊

git commit --amend --author="Author Name <[email protected]>"

6.遠程

(1)更改遠程倉庫地址

git remote set-url origin http://xxx.xx.com/x.git

(2)查看遠程倉庫日誌

git log origin/master

(3)比較本地分支和遠程分支

git diff master origin/master

(4)查看遠程分支

git branch -r

(5)拉取遠程

方法1:拉取併合並

git pull

方法2:先拉取後合并

git fetch origin

git merge origin/master

7.分支

查看分支:git branch

查看遠程分支:git branch -r

建立分支:git branch <name>

切換分支:git checkout <name>

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

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

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

刪除遠程分支:git push origin --delete <name>

8.儲藏

儲存:git stash

查看stash列表:git stash list

恢複:git stash apply

恢複特定版本:git stash apply [email protected]{2}

清除某個stash記錄:git stash drop [email protected]{0}

9.引用

(1)查看提交的雜湊字串

git log

git log --pretty=oneline

(2)查看某個提交

git show 0c708f(雜湊)

(3)查看分支、標籤或間接引用的雜湊

git rev-parse master

(4)查看引用

在.git/refs目錄下

git show refs/heads/some-feature

在.git目錄下還有一些引用:

  • HEAD – 當前所在的提交或分支。
  • FETCH_HEAD – 遠程倉庫中fetch到的最新一次提交。
  • ORIG_HEAD – HEAD的備份引用,避免損壞。
  • MERGE_HEAD – 你通過git merge併入當前分支的引用(們)。
  • CHERRY_PICK_HEAD – 你cherry pick使用的引用。

(5)refspec

refspec的定義是這樣的:[+]<src>:<dst>。<src>參數是本地的源分支,<dst>是遠端目標分支。可選的+號強制遠程倉庫採用非快速向前的更新策略。

(6)相對參照

git show HEAD~2 祖父節點

git show HEAD^2 第二個父節點(分支合并時,原所在分支是第一個父節點)

git show HEAD^2^1 可以多層

(7)引用日誌

git reflog

git checkout  [email protected]{1}

10.代碼復原

reset checkout revert的選擇

reset checkout 可以指定檔案或某次提交,revert只針對提交,不針對檔案

checkout revert 有可能會重寫檔案,在使用前要提交或緩衝工作目錄的修改

(1)reset(僅僅用在私人分支上,一般只用HEAD)

git checkout feature

git reset HEAD (末端兩個提交變成懸掛提交,git記憶體回收時會被刪除)

git reset --hard HEAD

  • --soft – 緩衝區和工作目錄都不會被改變
  • --mixed – 預設選項。緩衝區和你指定的提交同步,但工作目錄不受影響
  • --hard – 緩衝區和工作目錄都同步到你指定的提交

git reset HEAD file1.js 將HEAD提交中的該檔案加入到緩衝區,針對檔案沒有--mixed之類的參數

(2)checkout

git checkout feature 切換分支

git checkout HEAD~2 把HEAD移動到特定提交,但會造成HEAD分離,非常危險,如果你接著添加新的提交,然後切換到別的分支之後就沒辦法回到之前添加的這些提交。因此,在為分離的HEAD添加新的提交的時候你應該建立一個新的分支。

git checkout HEAD~2 file1.js 將工作目錄的該檔案同步到HEAD~2

(和git reset HEAD --hard很像,但隻影響特定檔案)

(3)revert(可以用在公用分支,不會影響曆史,建立一個新的提交來撤銷某個提交的修改)

git checkout feature

git revert HEAD~2 

11.打標籤

(1)查看當前標籤

git tag

(2)打標籤

git tag -a v1.0.0 -m "my version v1.0.0"

(3)查看某個tag

git show v1.0.0

(4)刪除某個tag

git tag -d v1.0.0

(4)把tag推送到遠程

git push origin --tags

12.比較

git diff  工作區(W)跟暫存區(I)的區別

git diff HEAD 工作區(W)跟本地倉庫(L)的區別

git diff --staged 暫存區(I)跟本地倉庫(L)的區別

(後邊跟上檔案名稱則只對比某個檔案)

13.打包

git archive --format zip --output /path/to/file.zip master

三、常用情境:

1.建立新的庫

mkdir tutorial

cd tutorial

git init

touch README.md

git add README.md

git commit -m "first commit"

git remote add origin http://xxx.xx.com/x.git

git push -u origin master

2.push已經存在的庫

cd existing_repo

git remote add origin http://xxx.xx.com/x.git

git push -u origin master

 

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.