身为技术人员,都知道Git是干吗的。从服务端角度它是代码仓库,能够多人协做、版本控制、高效处理大型或小型项目全部内容;从客户端讲,它可以方便管理本地分支、且与服务端代码的同步,从拉取、合并、提交等等管理分支都靠它!git
Git轻量、易于学习,若是不用搭建和维护代码仓库的话(运维职责),只要掌握几个git经常使用命令便可在工做中轻松应对。缓存
下面简单介绍几个概念,同时列出工做中经常使用命令:bash
快速入门,弄明白如下几个概念便可:运维
.git
,这个不是工做区,而是Git的版本库;git add
能够把要提交的内容放到暂存区;git commit
把暂存区全部内容提交到当前分支;工做中,通常咱们提交代码只要四步:学习
git pull
拉取代码,提交代码前确保和服务端仓库一致,避免冲突;git add ./your_file.txt
把文件添加进去,实际就是从工做区提交到暂存区;git commit -m 'first commit'
提交更改,再把暂存区全部内容提交到当前分支(默认master);git push [remoteName]
推送到远程仓库,也就是推到服务端,这样别人就能拉取pull
你的代码;平时工做也就用到上面四个步骤,固然了凡事有例外,下面说几个例外的处理办法:3d
git checkout <branch>
:切换到你须要的分支(dev、hotfix)版本控制
git checkout -b <branch>
: 若是没有分支,加上-b参数表示建立并切换;rest
参考连接:https://git-scm.com/docs/git-checkoutcode
撤销得分三种状况:教程
git add
的撤销方法;.gitignore
文件修改以后且没有git add
,直接经过git checkout -- <file>
撤销;λ git status On branch master Your branch is up-to-date with 'origin/master'. nothing to commit, working tree clean D:\learning\git\work (master -> origin) λ git status On branch master Your branch is up-to-date with 'origin/master'. 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: .gitignore no changes added to commit (use "git add" and/or "git commit -a") D:\learning\git\work (master -> origin) λ git checkout -- .gitignore D:\learning\git\work (master -> origin) λ git status On branch master Your branch is up-to-date with 'origin/master'. nothing to commit, working tree clean
扩展:
命令git checkout -- .gitignore
意思就是,把.gitignore文件在工做区的修改所有撤销,这里有两种状况:
一种是.gitignore自修改后尚未被放到暂存区,如今撤销修改就回到和版本库如出一辙的状态;
一种是.gitignore已经添加到暂存区,又做了修改,如今,撤销修改就回到添加到暂存区后的状态。
总之,就是让这个文件回到最近一次git commit或git add时的状态。
参考连接:https://www.liaoxuefeng.com/wiki/896043488029600/897889638509536
git add
的撤销方法git reset .gitignore
撤销到未git add
状态,再执行第一步便可。λ git add .gitignore D:\learning\git\work (master -> origin) λ 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) modified: .gitignore D:\learning\git\work (master -> origin) λ git reset .gitignore Unstaged changes after reset: M .gitignore D:\learning\git\work (master -> origin) λ git status On branch master Your branch is up-to-date with 'origin/master'. 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: .gitignore no changes added to commit (use "git add" and/or "git commit -a") D:\learning\git\work (master -> origin) λ git checkout -- .gitignore D:\learning\git\work (master -> origin) λ git status On branch master Your branch is up-to-date with 'origin/master'. nothing to commit, working tree clean
git reset --hard commitid
直接回到未修改状态。λ git add .gitignore λ git commit -m "test" #(省略无用部分) λ 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 D:\learning\git\work (master -> origin) λ git log commit b7de9378f39834dbc8304d4a8d30f39a4003c673 (HEAD -> master) Author: test <test@163.com> Date: Mon Sep 14 02:59:02 2020 +0800 test commit b3ed1078e543cdb26b984dac584df9db7553d506 (origin/master, origin/HEAD) Author: test <test@163.com> Date: Mon Sep 14 02:39:54 2020 +0800 09142020 D:\learning\git\work (master -> origin) λ git reset --hard b3ed1078e543cdb26b984dac584df9db7553d506 HEAD is now at b3ed107 09142020 D:\learning\git\work (master -> origin) λ git log commit b3ed1078e543cdb26b984dac584df9db7553d506 (HEAD -> master, origin/master, origin/HEAD) Author: test <test@163.com> Date: Mon Sep 14 02:39:54 2020 +0800 09142020 D:\learning\git\work (master -> origin) λ git status On branch master Your branch is up-to-date with 'origin/master'. nothing to commit, working tree clean
git stash
能够将你已经修改,但不想提交(git push)的代码临时保存到堆栈中,也就是回归到你git pull
时的状态。而后就能随意切换分支救火,完成后切换回来再git push pop
便可恢复以前的修改内容。stash
不只能够恢复到原先开发的分支,也能够恢复到其余任意指定的分支上(可跨分支)。
git status
,会发现当前是一个干净的工做区,没有任何改动。git stash list
显示保存进度的列表。也就意味着,git stash命令能够屡次执行。
git stash pop
恢复最新的进度到工做区。git默认会把工做区和暂存区的改动都恢复到工做区。
git stash pop stash@{stash_id}
恢复指定的进度到工做区。stash_id经过git stash list命令获得的
经过git stash pop命令恢复进度后,会删除当前进度。
git stash drop stash@{stash_id}
可使用git stash drop命令,后面能够跟stash_id
或使用git stash clear命令,删除全部缓存的stash
git stash show
查看堆栈中最新保存的stash和当前目录的差别
git add
以后再stash发现工做区是干净的;git stash pop
恢复。λ git status On branch master Your branch is up to date with 'origin/master'. Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git restore <file>..." to discard changes in working directory) modified: .gitignore Untracked files: (use "git add <file>..." to include in what will be committed) test.txt no changes added to commit (use "git add" and/or "git commit -a") D:\learning\git\timed_tasks (master -> origin) λ git stash Saved working directory and index state WIP on master: 542a055 create .gitignore D:\learning\git\timed_tasks (master -> origin) λ 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) test.txt nothing added to commit but untracked files present (use "git add" to track) D:\learning\git\timed_tasks (master -> origin) λ git add test.txt D:\learning\git\timed_tasks (master -> origin) λ git stash Saved working directory and index state WIP on master: 542a055 create .gitignore D:\learning\git\timed_tasks (master -> origin) λ git stash list stash@{0}: WIP on master: 542a055 create .gitignore stash@{1}: WIP on master: 542a055 create .gitignore stash@{2}: WIP on (no branch): 542a055 create .gitignore D:\learning\git\timed_tasks (master -> origin) λ git status On branch master Your branch is up to date with 'origin/master'. nothing to commit, working tree clean
D:\learning\git\timed_tasks (master -> origin) λ git stash show test.txt | 0 1 file changed, 0 insertions(+), 0 deletions(-) D:\learning\git\timed_tasks (master -> origin) λ git stash pop On branch master Your branch is up to date with 'origin/master'. Changes to be committed: (use "git restore --staged <file>..." to unstage) new file: test.txt Dropped refs/stash@{0} (b69da2894d5e7f511be18277c5a0cd4582fbf453) D:\learning\git\timed_tasks (master -> origin) λ git status On branch master Your branch is up to date with 'origin/master'. Changes to be committed: (use "git restore --staged <file>..." to unstage) new file: test.txt
Tip:若是你修改的全部文件都不想要了怎么办?可经过git stash清空,懂吧?
λ git status On branch master Your branch is up to date with 'origin/master'. Changes to be committed: (use "git restore --staged <file>..." to unstage) new file: test.txt D:\learning\git\timed_tasks (master -> origin) λ git stash Saved working directory and index state WIP on master: 542a055 create .gitignore D:\learning\git\timed_tasks (master -> origin) λ git stash clear D:\learning\git\timed_tasks (master -> origin) λ git status On branch master Your branch is up to date with 'origin/master'. nothing to commit, working tree clean
----by 钢铁 648403020@qq.com
参考资料:
Git官方文档:https://git-scm.com/docs
廖雪峰Git教程:https://www.liaoxuefeng.com/wiki/896043488029600