1. 程式人生 > >git 常用命令 mv rm checkout revert reset

git 常用命令 mv rm checkout revert reset

discard war reat lap .com eve 版本庫 查詢 git 常用命令

關於上節講的git add 時需要添加註釋信息,也可以在git commit時再添加

[email protected] 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:關於註釋信息, 因為一個項目往往由多個人協作完成,所以文件的提交和修改需要在註釋信息裏寫清楚,否則會影響到項目的展開。

git log 命令查看commit的記錄:

$ 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命令

$ 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刪除,有多種處理方法:1、在工作區創建個同名的文件,然後add到暫存區。2、使用 git rm 刪除暫存區的這個文件。3、使用 git checkout恢復之前刪除的文件(類似回滾).

技術分享
$ vim abc

[email protected]-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (master)
$ ls
abc  bb.css  dd.css  index.html  UI.js

[email protected]-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (master)
$ cat abc
fsdfsdfsdf

[email protected]-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)

[email protected]-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.

[email protected]-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


[email protected]-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (master)
$ rm abc

[email protected]-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


[email protected]-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (master)
$
例子 技術分享
$ git checkout -- abc

[email protected]-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


[email protected]-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (master)
$ ls
abc  bb.css  dd.css  index.html  UI.js

[email protected]-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (master)
$ cat abc
fsdfsdfsdf
git checkout --

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

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

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

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


[email protected]-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

[email protected]-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (master)
$ git status
On branch master
nothing to commit, working tree clean

[email protected]-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (master)
$ git rm abc
rm abc

[email protected]-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


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

[email protected]-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (master)
$ git status
On branch master
nothing to commit, working tree clean

[email protected]-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

[email protected]-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (master)
$ ls
abc  bb.css  dd.css  index.html  UI.js

[email protected]-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(指針)可以用版本號來替代。

[email protected] 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

[email protected]-TPPLHIB MINGW64 /c/laoni/PycharmProjects/github_test (master)
$ git reset 358cdac

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

git 常用命令 mv rm checkout revert reset