git 常規操做以及回滾操做

在這裏記錄一些平時比較經常使用的git的命令行操做,以及一些回滾的操做,以提升咱們平時的開發效率。git

首先個人 git 項目倉庫在 bitbucket 上面,因此我在 bitbucket 上面建立一個空的項目。項目的名字就叫test1。dom

建立完項目咱們會獲得一個項目的倉庫地址,就在圖片中 Overview 這幾個字的下面。fetch

先來介紹一下 git 的幾個概念。命令行

* 工做區code

* 暫存區圖片

* 本地倉庫開發

* 遠程倉庫rem

工做區即咱們的開發環境。字符串

暫存區即全部文件能被git追蹤的環境。同步

本地倉庫顧名思義即本地的一個項目倉庫,每一個開發者都有一個獨立的本地的倉庫。

遠程倉庫全部代碼的最終歸屬地。

再來一張 git 命令的簡要說明。

 

準備工做作完以後下面就開始介紹命令(如下操做都在命令行下完成)。

1.下載項目

git clone <項目地址>

bingaos-MBP:Desktop bingao# git clone https://Mike244483@bitbucket.org/Mike244483/test1.git
Cloning into 'test1'...
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.

在當前目錄下會有一個test1的目錄,進入目錄能夠看到剛纔建立項目自動生成的README.md文件。

bingaos-MBP:Desktop bingao# cd test1/
bingaos-MBP:test1 bingao$ ls -l
total 8
-rw-r--r--  1 bingao  staff  9 Feb 21 14:17 README.md
bingaos-MBP:test1 bingao#

 

2.從遠程庫獲取最新的代碼

git fetch

 

3.下載最近代碼

git pull

 

4.建立一個文件並提交到遠程倉庫

這裏我先建立一個 a.txt 內容爲 123

bingaos-MBP:test1 bingao# echo 123 > a.txt
bingaos-MBP:test1 bingao# ls -l
total 16
-rw-r--r--  1 bingao  staff  9 Feb 21 14:17 README.md
-rw-r--r--  1 bingao  staff  4 Feb 21 14:32 a.txt
bingaos-MBP:test1 bingao#

使用 git status 查看當前項目狀態

bingaos-MBP:test1 bingao# git status
On branch master
Your branch is up-to-date with 'origin/master'.

Untracked files:
  (use "git add <file>..." to include in what will be committed)

	a.txt

nothing added to commit but untracked files present (use "git add" to track)
bingaos-MBP:test1 bingao#

根據提示能夠看到我當前的分支是 master 分支, 有一個 a.txt 文件尚未被git追蹤

那根據提示先使用 git add 把 a.txt 加入到 git 倉庫,使 git 能夠感知到文件的變化。

git add <filename>  #添加指定文件到 git 倉庫

git add . #添加當前目錄到 git 倉庫

bingaos-MBP:test1 bingao# git add a.txt 
bingaos-MBP:test1 bingao# git status
On branch master
Your branch is up-to-date with 'origin/master'.

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

	new file:   a.txt

bingaos-MBP:test1 bingao#

再次使用 git status 能夠看到提示說 a.txt 能夠被提交到暫存區。

使用 git commit -m '<comments>' 命令提交全部暫存區的文件到本地倉庫。

bingaos-MBP:test1 bingao# git commit -m 'add a.txt'
[master 1289dee] add a.txt
 1 file changed, 1 insertion(+)
 create mode 100644 a.txt
bingaos-MBP:test1 bingao#

再次使用 git status 命令

bingaos-MBP:test1 bingao# git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
  (use "git push" to publish your local commits)

nothing to commit, working tree clean
bingaos-MBP:test1 bingao#

能夠看到提示 個人分支比遠程分支的 master 分支提早一個版本。

使用 git push 來推送本地倉庫的修改到遠程倉庫。

bingaos-MBP:test1 bingao# git push
Counting objects: 3, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 276 bytes | 276.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://bitbucket.org/Mike244483/test1.git
   9b49f08..1289dee  master -> master
bingaos-MBP:test1 bingao#

好了,這樣在遠程分支就能夠看到建立的 a.txt 文件了。

 

5.查看分支

git branch #查看當前分支

git branch -r #查看遠程分支

git branch -a #查看全部分支

bingaos-MBP:test1 bingao# git branch
* master
bingaos-MBP:test1 bingao# git branch -r
  origin/HEAD -> origin/master
  origin/master
bingaos-MBP:test1 bingao# git branch -a
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/master
bingaos-MBP:test1 bingao#

 

6. 建立分支

git branch <新的分支號>

bingaos-MBP:test1 bingao# git branch v1.0.0
bingaos-MBP:test1 bingao# git branch
* master
  v1.0.0
bingaos-MBP:test1 bingao#

 

7.提交本地分支到遠程倉庫

git push 的時候會提示說遠程倉庫沒有叫 v1.0.0 的分支 要提交的話需加上 --set-upstream 參數。

bingaos-MBP:test1 bingao# git push
fatal: The current branch v1.0.0 has no upstream branch.
To push the current branch and set the remote as upstream, use

    git push --set-upstream origin v1.0.0

bingaos-MBP:test1 bingao# git push --set-upstream origin v1.0.0
Counting objects: 3, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 298 bytes | 298.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
remote: 
remote: Create pull request for v1.0.0:
remote:   https://bitbucket.org/Mike244483/test1/pull-requests/new?source=v1.0.0&t=1
remote: 
To https://bitbucket.org/Mike244483/test1.git
 * [new branch]      v1.0.0 -> v1.0.0
Branch v1.0.0 set up to track remote branch v1.0.0 from origin.
bingaos-MBP:test1 bingao#

 

8.切換分支

git checkout <分支號>

bingaos-MBP:test1 bingao# git checkout v1.0.0
Switched to branch 'v1.0.0'
bingaos-MBP:test1 bingao$ git branch
  master
* v1.0.0
bingaos-MBP:test1 bingao#

 

9.查看提交歷史

git log

bingaos-MBP:test1 bingao# git log
commit 1289deec34cdde05f25f5891b48b95d0645eca37 (HEAD -> v1.0.0, origin/master, origin/HEAD, master)
Author: Michael <wudongming1021@126.com>
Date:   Wed Feb 21 14:57:08 2018 +0800

    add a.txt

commit 9b49f080b675ee58fb0f2ea68949928f0143a434
Author: Mike <wudongming1021@gmail.com>
Date:   Wed Feb 21 06:13:38 2018 +0000

    Initial commit
bingaos-MBP:test1 bingao#

 

經常使用操做介紹完了,下面介紹 git 的各類回滾操做。爲了方便演示先建立了一些文件。

master 分支爲以下內容

README.md, a.txt

v1.0.0 分支爲以下內容

README.md, a.txt, b.txt

 

1.撤銷本地修改

當修改了一波代碼以後發現代碼寫得很挫不要了,直接恢復到當前分支的最新版本。

git checkout <filename>

bingaos-MBP:test1 bingao# ll
total 24
-rw-r--r--  1 bingao  staff  9 Feb 21 14:17 README.md
-rw-r--r--  1 bingao  staff  4 Feb 21 14:32 a.txt
-rw-r--r--  1 bingao  staff  4 Feb 21 15:13 b.txt
bingaos-MBP:test1 bingao# cat b.txt 
456
bingaos-MBP:test1 bingao# echo qwe >> b.txt 
bingaos-MBP:test1 bingao# cat b.txt 
456
qwe
bingaos-MBP:test1 bingao# git checkout b.txt 
bingaos-MBP:test1 bingao# cat b.txt 
456
bingaos-MBP:test1 bingao#

b.txt 原本的內容爲 456, 在文件末尾追加了qwe。使用 git checkout b.txt 直接將文件內容恢復到 456。

 

2.本地修改已經提交到暫存區,從暫存區撤銷文件回到工做區。

git reset <filename>

bingaos-MBP:test1 bingao# cat b.txt 
456
bingaos-MBP:test1 bingao# echo qwe >> b.txt 
bingaos-MBP:test1 bingao# cat b.txt 
456
qwe
bingaos-MBP:test1 bingao# git add b.txt 
bingaos-MBP:test1 bingao# git status
On branch v1.0.0
Your branch is up-to-date with 'origin/v1.0.0'.

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    modified:   b.txt

bingaos-MBP:test1 bingao# git reset b.txt 
Unstaged changes after reset:
M   b.txt
bingaos-MBP:test1 bingao# git status
On branch v1.0.0
Your branch is up-to-date with 'origin/v1.0.0'.

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:   b.txt

no changes added to commit (use "git add" and/or "git commit -a")
bingaos-MBP:test1 bingao# cat b.txt
456
qwe

追加 qwe 到 b.txt,而且執行了 git add 提交到暫存區。 使用 git reset b.txt 從暫存區撤銷,但文件內容保持保留不變。

 

3.撤銷已提交到本地倉庫的提交

git reset <上一次commit id>

bingaos-MBP:test1 bingao# git log
commit c2eb27ec8c1d1911b4b79a9a7cc4063fb3ad98be (HEAD -> v1.0.0, origin/v1.0.0)
Author: Michael <wudongming1021@126.com>
Date:   Wed Feb 21 15:13:44 2018 +0800

    b.txt

commit 1289deec34cdde05f25f5891b48b95d0645eca37 (origin/master, origin/HEAD, master)
Author: Michael <wudongming1021@126.com>
Date:   Wed Feb 21 14:57:08 2018 +0800

    add a.txt

commit 9b49f080b675ee58fb0f2ea68949928f0143a434
Author: Mike <wudongming1021@gmail.com>
Date:   Wed Feb 21 06:13:38 2018 +0000

    Initial commit
bingaos-MBP:test1 bingao# cat b.txt 
456
bingaos-MBP:test1 bingao# echo qwe >> b.txt
bingaos-MBP:test1 bingao# cat b.txt
456
qwe
bingaos-MBP:test1 bingao# git add b.txt 
bingaos-MBP:test1 bingao# git commit -m 'add b.txt'
[v1.0.0 058d31e] add b.txt
 1 file changed, 1 insertion(+)
bingaos-MBP:test1 bingao# git status
On branch v1.0.0
Your branch is ahead of 'origin/v1.0.0' by 1 commit.
  (use "git push" to publish your local commits)

nothing to commit, working tree clean
bingaos-MBP:test1 bingao# git log
commit 058d31eb7dd9a4ba525643652235643e7f330e17 (HEAD -> v1.0.0)
Author: Michael <wudongming1021@126.com>
Date:   Wed Feb 21 15:33:30 2018 +0800

    add b.txt

commit c2eb27ec8c1d1911b4b79a9a7cc4063fb3ad98be (origin/v1.0.0)
Author: Michael <wudongming1021@126.com>
Date:   Wed Feb 21 15:13:44 2018 +0800

    b.txt

commit 1289deec34cdde05f25f5891b48b95d0645eca37 (origin/master, origin/HEAD, master)
Author: Michael <wudongming1021@126.com>
Date:   Wed Feb 21 14:57:08 2018 +0800

    add a.txt

commit 9b49f080b675ee58fb0f2ea68949928f0143a434
Author: Mike <wudongming1021@gmail.com>
Date:   Wed Feb 21 06:13:38 2018 +0000

    Initial commit
bingaos-MBP:test1 bingao#

這裏我將 qwe 追加到 b.txt 文件而且已經提交到本地倉庫。使用 git log 看到 HEAD 指向的v1.0.0是我剛纔修改的,而遠程倉庫上的v1.0.0分支的版本號是 c2eb27ec8c1d1911b4b79a9a7cc4063fb3ad98be。咱們將本地倉庫的此次提交撤銷回滾到遠程倉庫的分支。

bingaos-MBP:test1 bingao# git reset c2eb27ec8c1d1911b4b79a9a7cc4063fb3ad98be
Unstaged changes after reset:
M	b.txt
bingaos-MBP:test1 bingao# cat b.txt 
456
qwe
bingaos-MBP:test1 bingao# git status
On branch v1.0.0
Your branch is up-to-date with 'origin/v1.0.0'.

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:   b.txt

no changes added to commit (use "git add" and/or "git commit -a")
bingaos-MBP:test1 bingao# git log
commit c2eb27ec8c1d1911b4b79a9a7cc4063fb3ad98be (HEAD -> v1.0.0, origin/v1.0.0)
Author: Michael <wudongming1021@126.com>
Date:   Wed Feb 21 15:13:44 2018 +0800

    b.txt

commit 1289deec34cdde05f25f5891b48b95d0645eca37 (origin/master, origin/HEAD, master)
Author: Michael <wudongming1021@126.com>
Date:   Wed Feb 21 14:57:08 2018 +0800

    add a.txt

commit 9b49f080b675ee58fb0f2ea68949928f0143a434
Author: Mike <wudongming1021@gmail.com>
Date:   Wed Feb 21 06:13:38 2018 +0000

    Initial commit
bingaos-MBP:test1 bingao#

從提交歷史中能夠看到剛纔的commit ID已經不見了,b.txt 也回到了工做區的狀態,文件內容也保留。若是想要放棄文件內容使用 git reset --hard <上一次commit id> 便可。

 

4.撤銷已提交到遠程倉庫的提交

這種撤銷有2個方式能夠實現。

一種方式是使用 git reset --hard; git push --force。這種方式是強制回滾到指定的版本再強制提交到遠程倉庫。這種方式在2次提交中間的提交歷史會看不到。

bingaos-MBP:test1 bingao# echo asdfasdf >> b.txt
bingaos-MBP:test1 bingao# cat b.txt 
456
qwe
asdfasdf
bingaos-MBP:test1 bingao# git add b.txt 
bingaos-MBP:test1 bingao# git commit -m 'random update b.txt'
[v1.0.0 83f68fd] random update b.txt
 1 file changed, 2 insertions(+)
bingaos-MBP:test1 bingao# git push
Counting objects: 3, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 270 bytes | 270.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: 
remote: Create pull request for v1.0.0:
remote:   https://bitbucket.org/Mike244483/test1/pull-requests/new?source=v1.0.0&t=1
remote: 
To https://bitbucket.org/Mike244483/test1.git
   c2eb27e..83f68fd  v1.0.0 -> v1.0.0
bingaos-MBP:test1 bingao# git log
commit 83f68fd76d37aca072547661d134c9c25220065b (HEAD -> v1.0.0, origin/v1.0.0)
Author: Michael <wudongming1021@126.com>
Date:   Wed Feb 21 15:51:31 2018 +0800

    random update b.txt

commit c2eb27ec8c1d1911b4b79a9a7cc4063fb3ad98be
Author: Michael <wudongming1021@126.com>
Date:   Wed Feb 21 15:13:44 2018 +0800

    b.txt

commit 1289deec34cdde05f25f5891b48b95d0645eca37 (origin/master, origin/HEAD, master)
Author: Michael <wudongming1021@126.com>
Date:   Wed Feb 21 14:57:08 2018 +0800

    add a.txt

commit 9b49f080b675ee58fb0f2ea68949928f0143a434
Author: Mike <wudongming1021@gmail.com>
Date:   Wed Feb 21 06:13:38 2018 +0000

    Initial commit
bingaos-MBP:test1 bingao#

這裏我提交了一個 random update b.txt, 隨意的修改了b.txt文件而且已經提交到了遠程分支(使用 git log 看到HEAD和origin都是指向的我剛纔那個提交)。如今我想回滾到 c2eb27ec8c1d1911b4b79a9a7cc4063fb3ad98be 這個分支。

bingaos-MBP:test1 bingao# git reset --hard c2eb27ec8c1d1911b4b79a9a7cc4063fb3ad98be
HEAD is now at c2eb27e b.txt
bingaos-MBP:test1 bingao# git push --force
Total 0 (delta 0), reused 0 (delta 0)
remote: 
remote: Create pull request for v1.0.0:
remote:   https://bitbucket.org/Mike244483/test1/pull-requests/new?source=v1.0.0&t=1
remote: 
To https://bitbucket.org/Mike244483/test1.git
 + 83f68fd...c2eb27e v1.0.0 -> v1.0.0 (forced update)
bingaos-MBP:test1 bingao# git log
commit c2eb27ec8c1d1911b4b79a9a7cc4063fb3ad98be (HEAD -> v1.0.0, origin/v1.0.0)
Author: Michael <wudongming1021@126.com>
Date:   Wed Feb 21 15:13:44 2018 +0800

    b.txt

commit 1289deec34cdde05f25f5891b48b95d0645eca37 (origin/master, origin/HEAD, master)
Author: Michael <wudongming1021@126.com>
Date:   Wed Feb 21 14:57:08 2018 +0800

    add a.txt

commit 9b49f080b675ee58fb0f2ea68949928f0143a434
Author: Mike <wudongming1021@gmail.com>
Date:   Wed Feb 21 06:13:38 2018 +0000

    Initial commit
bingaos-MBP:test1 bingao$

當執行完 git reset; git push 以後能夠看到 git log 中已看不到剛纔的 random update b.txt, 文件已回滾到指定的版本。

 

第二種方式是使用 git revert; git push。git revert 是使用一次新的提交來撤銷指定版本,而且在提交歷史中能夠查到相關操做記錄。

bingaos-MBP:test1 bingao# cat b.txt 
456
bingaos-MBP:test1 bingao# echo zxcv >> b.txt 
bingaos-MBP:test1 bingao# cat b.txt 
456
zxcv
bingaos-MBP:test1 bingao# git add b.txt 
bingaos-MBP:test1 bingao# git commit -m 'random update 2 b.txt'
[v1.0.0 319ca45] random update 2 b.txt
 1 file changed, 1 insertion(+)
bingaos-MBP:test1 bingao# git push
Counting objects: 3, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 267 bytes | 267.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: 
remote: Create pull request for v1.0.0:
remote:   https://bitbucket.org/Mike244483/test1/pull-requests/new?source=v1.0.0&t=1
remote: 
To https://bitbucket.org/Mike244483/test1.git
   c2eb27e..319ca45  v1.0.0 -> v1.0.0
bingaos-MBP:test1 bingao# git log
commit 319ca45e084a7cee031b0e19571f640efcd73328 (HEAD -> v1.0.0, origin/v1.0.0)
Author: Michael <wudongming1021@126.com>
Date:   Wed Feb 21 15:58:26 2018 +0800

    random update 2 b.txt

commit c2eb27ec8c1d1911b4b79a9a7cc4063fb3ad98be
Author: Michael <wudongming1021@126.com>
Date:   Wed Feb 21 15:13:44 2018 +0800

    b.txt

commit 1289deec34cdde05f25f5891b48b95d0645eca37 (origin/master, origin/HEAD, master)
Author: Michael <wudongming1021@126.com>
Date:   Wed Feb 21 14:57:08 2018 +0800

    add a.txt

commit 9b49f080b675ee58fb0f2ea68949928f0143a434
Author: Mike <wudongming1021@gmail.com>
Date:   Wed Feb 21 06:13:38 2018 +0000

    Initial commit
bingaos-MBP:test1 bingao# cat b.txt 
456
zxcv
bingaos-MBP:test1 bingao#

這裏我又作了一次提交 修改了 b.txt 的內容,追加了 zxcv (任意字符串)。

此次使用 git revert 操做

bingaos-MBP:test1 bingao# git revert 319ca45e084a7cee031b0e19571f640efcd73328
[v1.0.0 fb0fe30] Revert "random update 2 b.txt"
 1 file changed, 1 deletion(-)
bingaos-MBP:test1 bingao# git push
Counting objects: 3, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 297 bytes | 297.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: 
remote: Create pull request for v1.0.0:
remote:   https://bitbucket.org/Mike244483/test1/pull-requests/new?source=v1.0.0&t=1
remote: 
To https://bitbucket.org/Mike244483/test1.git
   319ca45..fb0fe30  v1.0.0 -> v1.0.0
bingaos-MBP:test1 bingao# git log
commit fb0fe3053fed1bb374a834151c17f30a5b6df54b (HEAD -> v1.0.0, origin/v1.0.0)
Author: Michael <wudongming1021@126.com>
Date:   Wed Feb 21 16:00:21 2018 +0800

    Revert "random update 2 b.txt"
    
    This reverts commit 319ca45e084a7cee031b0e19571f640efcd73328.
    
    revert random update 2

commit 319ca45e084a7cee031b0e19571f640efcd73328
Author: Michael <wudongming1021@126.com>
Date:   Wed Feb 21 15:58:26 2018 +0800

    random update 2 b.txt

commit c2eb27ec8c1d1911b4b79a9a7cc4063fb3ad98be
Author: Michael <wudongming1021@126.com>
Date:   Wed Feb 21 15:13:44 2018 +0800

    b.txt

commit 1289deec34cdde05f25f5891b48b95d0645eca37 (origin/master, origin/HEAD, master)
Author: Michael <wudongming1021@126.com>
Date:   Wed Feb 21 14:57:08 2018 +0800

    add a.txt

commit 9b49f080b675ee58fb0f2ea68949928f0143a434
Author: Mike <wudongming1021@gmail.com>
Date:   Wed Feb 21 06:13:38 2018 +0000

    Initial commit
bingaos-MBP:test1 bingao# cat b.txt 
456
bingaos-MBP:test1 bingao#

經過 git log 能夠看到 random update 2 b.txt 這個提交歷史還在,可是又多了一個 Revert "random update 2 b.txt" 這個提交歷史,這個就是剛纔執行的 git revert 操做。

 

須要注意的是 git reset 指定的 commit id 是須要回滾到的那個commit id。git revert 指定的 commit id 是須要被回滾的 commit id。

 

5.回滾到上一個版本

git reset HEAD^ # ^爲上一個版本 ^^爲上二個版本 ^^^爲上三個版本

如需廢棄現有的修改 git reset --hard HEAD^

 

6.強制覆蓋本地

git reset --hard origin/<branch_name>

使用遠程分支覆蓋本地修改

 

7.回滾到指定版本

git reset <commit id>

git reset —hard <commit id>

 

8.刪除遠程分支上的文件

git rm <filename>

git commit -m ''

git push

 

9.git rebase / git merge

這2個命令就作下說明。git merge 將我分支上的修改內容合併到別的分支上。git rebase, 如我建立了一個分支編寫一個新功能,可是花的時間比較長已與主分支版本有很大的不一樣,這時候可使用 git rebase 把主分支上的修改同步到個人分支上。

相關文章
相關標籤/搜索