git 常用命令 mv rm checkout revert reset
关于上节讲的git add 时需要添加注释信息,也可以在git commit时再添加
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:关于注释信息, 因为一个项目往往由多个人协作完成,所以文件的提交和修改需要在注释信息里写清楚,否则会影响到项目的展开。
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 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 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 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 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 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