git 經常使用命令 mv rm checkout revert reset

    關於上節講的git add 時須要添加註釋信息,也能夠在git commit時再添加css

laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (master)
$ git add UI.js

laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (master)
$ git commit -m "添加UI.js"
[master 358cdac] 添加UI.js
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 UI.js

laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (master)
$ git status
On branch master
nothing to commit, working tree clean

PS:關於註釋信息, 由於一個項目每每由多我的協做完成,因此文件的提交和修改須要在註釋信息裏寫清楚,不然會影響到項目的展開。html

git log 命令查看commit的記錄:git

$ git log
commit 358cdace04c0eed4e4fab58a20070f5a7d01bb98
Author: LaoNiNi <laonivv@163.com>
Date:   Tue May 30 11:03:37 2017 +0800

    添加UI.js

commit 04c94a894c0895e6565a02b9f9221b51e3ca7bf5
Author: LaoNiNi <laonivv@163.com>
Date:   Tue May 30 10:44:49 2017 +0800

    添加一個文件index.html

laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (master)
$ git log --oneline
358cdac 添加UI.js
04c94a8 添加一個文件index.html

使用git mv 【old_filename】 【new_filename】修改的是暫存區的文件,不是工做區的,在工做區的文件能夠直接用mv命令github

$ ls
bb.css  cc.css  index.html  UI.js

laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (master)
$ git add cc.css

laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (master)
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        new file:   cc.css


laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (master)
$ git mv cc.css dd.css

laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (master)
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        new file:   dd.css


laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (master)
$ git commit -m "測試git mv"
[master 0f65134] 測試git mv
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 dd.css

laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (master)
$ git status
On branch master
nothing to commit, working tree clean

 當已經添加到暫存區的文件,若是對應的工做區的文件被rm刪除,有多種處理方法:一、在工做區建立個同名的文件,而後add到暫存區。二、使用 git rm 刪除暫存區的這個文件。三、使用 git checkout恢復以前刪除的文件(相似回滾).vim

$ vim abc

laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (master)
$ ls
abc  bb.css  dd.css  index.html  UI.js

laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (master)
$ cat abc
fsdfsdfsdf

laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (master)
$ git status
On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)

        abc

nothing added to commit but untracked files present (use "git add" to track)

laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (master)
$ git add abc
warning: LF will be replaced by CRLF in abc.
The file will have its original line endings in your working directory.

laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (master)
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        new file:   abc


laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (master)
$ rm abc

laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (master)
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        new file:   abc

Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        deleted:    abc


laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (master)
$
例子
$ git checkout -- abc

laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (master)
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        new file:   abc


laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (master)
$ ls
abc  bb.css  dd.css  index.html  UI.js

laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (master)
$ cat abc
fsdfsdfsdf
git checkout --

補充:使用git rm刪除暫存區的文件時,git會提示ide

(use --cached to keep the file, or -f to force removal)測試

--cached加上,只會刪除暫存區的文件,本地文件不刪除。-f則暫存區和本地都刪除。spa

git rm也能刪除分支上的文件(脫離版本庫),刪除事後能夠在git status裏查看到暫存區有這個被刪除的文件,須要git commit後,文件就被刪除了,使用git rm 時能夠加上--cached,保留本地文件。指針

$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        new file:   abc


laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (master)
$ git commit abc -m "commit abc"
[master 6d3b5d2] commit abc
 1 file changed, 1 insertion(+)
 create mode 100644 abc

laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (master)
$ git status
On branch master
nothing to commit, working tree clean

laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (master)
$ git rm abc
rm 'abc'

laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (master)
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        deleted:    abc


laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (master)
$ git commit -m "刪除分支上的abc"
[master 180917b] 刪除分支上的abc
 1 file changed, 1 deletion(-)
 delete mode 100644 abc

laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (master)
$ git status
On branch master
nothing to commit, working tree clean

laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (master)
$
git rm 分支上的文件

 使用git revert HEAD 能夠從當前版本撤銷返回到歷史版本,經過git log查看歷史版本,執行git revert HEAD默認返回到上一個版本,執行git revert HEAD^^ 返回到前兩個版本,執行git revert HEAD~3 返回到前三個歷史版本。日誌

$ git revert HEAD
[master 664ed39] Revert "刪除分支上的abc"
 1 file changed, 1 insertion(+)
 create mode 100644 abc

laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (master)
$ ls
abc  bb.css  dd.css  index.html  UI.js

laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (master)
$ cat abc
fsdfsdfsdf
git revert HEAD

git revert HEAD命令的提交能夠在提交日誌裏查到,而使用git reset HEAD命令在git log 裏查詢不到,因此git reset的執行有必定的危險(執行後不能恢復),但git reset只是把提交到分支的回撤到暫存區而不會刪除文件。使用mixed參數,git reset HEAD --mixed 那麼分支的文件就不是回撤到暫存區了,而是返回到工做區。使用hard參數,git reset HEAD --hard 就是硬重置,回撤的文件裏在工做區也早不到了,因此不要輕易使用硬重置,可使用軟重置。HEAD(指針)能夠用版本號來替代。

laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (master)
$ git log --oneline
664ed39 Revert "刪除分支上的abc"
180917b 刪除分支上的abc
6d3b5d2 commit abc
0f65134 測試git mv
822f28b 測試mv命令
358cdac 添加UI.js
04c94a8 添加一個文件index.html

laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (master)
$ git reset 358cdac

laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (master)
$ git log --oneline
358cdac 添加UI.js
04c94a8 添加一個文件index.html
相關文章
相關標籤/搜索