身为技术人员,都知道Git是干嘛的。从服务端角度它是代码仓库,可以多人协作、版本控制、高效处理大型或小型项目所有内容;从客户端讲,它能够方便管理本地分支、且与服务端代码的同步,从拉取、合并、提交等等管理分支都靠它!
Git轻量、易于学习,如果不用搭建和维护代码仓库的话(运维职责),只要掌握几个git常用命令即可在工作中轻松应对。
下面简单介绍几个概念,同时列出工作中常用命令:
快速入门,弄明白以下几个概念即可:
.git
,这个不是工作区,而是Git的版本库;git add
可以把要提交的内容放到暂存区;git commit
把暂存区所有内容提交到当前分支;工作中,一般我们提交代码只要四步:
git pull
拉取代码,提交代码前确保和服务端仓库一致,避免冲突;git add ./your_file.txt
把文件添加进去,实际就是从工作区提交到暂存区;git commit -m ‘first commit‘
提交更改,再把暂存区所有内容提交到当前分支(默认master);git push [remoteName]
推送到远程仓库,也就是推到服务端,这样别人就能拉取pull
你的代码;平时工作也就用到上面四个步骤,当然了凡事有例外,下面说几个例外的处理办法:
git checkout <branch>
:切换到你需要的分支(dev、hotfix)
git checkout -b <branch>
: 如果没有分支,加上-b参数表示创建并切换;
参考链接:https://git-scm.com/docs/git-checkout
撤销得分三种情况:
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
原文:https://www.cnblogs.com/jiba/p/13665946.html