身为技术人员,都知道Git是干嘛的。从服务端角度它是代码仓库,可以多人协作、版本控制、高效处理大型或小型项目所有内容;从客户端讲,它能够方便管理本地分支、且与服务端代码的同步,从拉取、合并、提交等等管理分支都靠它!
Git轻量、易于学习,如果不用搭建和维护代码仓库的话(运维职责),只要掌握几个git常用命令即可在工作中轻松应对。
下面简单介绍几个概念,同时列出工作中常用命令:
快速入门,弄明白以下几个概念即可:
工作中,一般我们提交代码只要四步:
平时工作也就用到上面四个步骤,当然了凡事有例外,下面说几个例外的处理办法:
git checkout
git checkout -b
参考链接:https://git-scm.com/docs/git-checkout
撤销得分三种情况:
λ git status
On branch masterYour branch is up-to-date with 'origin/master'.
nothing to commit, working tree clean
D:\learning\git\work (master -> origin)
λ git statusOn branch masterYour branch is up-to-date with 'origin/master'.
Changes not staged for commit:
(use "git add ..." to update what will be committed)
(use "git checkout -- ..." to discard changes in working directory)
modified: .gitignoreno 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 .gitignore
D:\learning\git\work (master -> origin)λ git statusOn branch masterYour branch is up-to-date with 'origin/master'.
Changes to be committed: (use "git reset HEAD ..." to unstage)
modified: .gitignoreD:\learning\git\work (master -> origin)λ git reset .gitignoreUnstaged changes after reset:M .gitignoreD:\learning\git\work (master -> origin)λ git statusOn branch masterYour branch is up-to-date with 'origin/master'.
Changes not staged for commit:
(use "git add ..." to update what will be committed)
(use "git checkout -- ..." to discard changes in working directory)
modified: .gitignoreno changes added to commit (use "git add" and/or "git commit -a")
D:\learning\git\work (master -> origin)λ git checkout -- .gitignoreD:\learning\git\work (master -> origin)λ git statusOn branch masterYour branch is up-to-date with 'origin/master'.
nothing to commit, working tree clean
λ git add .gitignore
λ git commit -m "test"
#(省略无用部分)λ git statusOn branch masterYour branch is ahead of 'origin/master' by 1 commit.
(use "git push" to publish your local commits)
nothing to commit, working tree cleanD:\learning\git\work (master -> origin)λ git logcommit b7de9378f39834dbc8304d4a8d30f39a4003c673 (HEAD -> master)Author: test
Date: Mon Sep 14 02:59:02 2020 +0800
testcommit b3ed1078e543cdb26b984dac584df9db7553d506 (origin/master, origin/HEAD)Author: test
Date: Mon Sep 14 02:39:54 2020 +0800
09142020
D:\learning\git\work (master -> origin)λ git reset --hard b3ed1078e543cdb26b984dac584df9db7553d506HEAD is now at b3ed107 09142020
D:\learning\git\work (master -> origin)λ git logcommit b3ed1078e543cdb26b984dac584df9db7553d506 (HEAD -> master, origin/master, origin/HEAD)Author: test
Date: Mon Sep 14 02:39:54 2020 +0800
09142020
D:\learning\git\work (master -> origin)λ git statusOn branch masterYour 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
On branch masterYour branch is up to date with 'origin/master'.
Changes not staged for commit:
(use "git add ..." to update what will be committed)
(use "git restore ..." to discard changes in working directory)
modified: .gitignoreUntracked files: (use "git add ..." to include in what will be committed)
test.txtno changes added to commit (use "git add" and/or "git commit -a")
D:\learning\git\timed_tasks (master -> origin)
λ git stashSaved working directory and index state WIP on master: 542a055 create .gitignore
D:\learning\git\timed_tasks (master -> origin)
λ git statusOn branch masterYour branch is up to date with 'origin/master'.
Untracked files: (use "git add ..." to include in what will be committed)
test.txtnothing 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 stashSaved working directory and index state WIP on master: 542a055 create .gitignore
D:\learning\git\timed_tasks (master -> origin)
λ git stash liststash@{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 statusOn branch masterYour 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 popOn branch masterYour branch is up to date with 'origin/master'.
Changes to be committed:
(use "git restore --staged ..." to unstage)
new file: test.txt
Dropped refs/stash@{0} (b69da2894d5e7f511be18277c5a0cd4582fbf453)
D:\learning\git\timed_tasks (master -> origin)
λ git statusOn branch masterYour branch is up to date with 'origin/master'.
Changes to be committed:
(use "git restore --staged ..." to unstage)
new file: test.txt
Tip:如果你修改的所有文件都不想要了怎么办?可通过git stash清空,懂吧?
λ git status
On branch masterYour branch is up to date with 'origin/master'.
Changes to be committed:
(use "git restore --staged ..." to unstage)
new file: test.txt
D:\learning\git\timed_tasks (master -> origin)
λ git stashSaved working directory and index state WIP on master: 542a055 create .gitignore
D:\learning\git\timed_tasks (master -> origin)
λ git stash clearD:\learning\git\timed_tasks (master -> origin)
λ git statusOn branch masterYour branch is up to date with 'origin/master'.
nothing to commit, working tree clean
λ git status
On branch masterYour branch is up to date with 'origin/master'.
Changes to be committed:
(use "git restore --staged ..." to unstage)
new file: test.txt
D:\learning\git\timed_tasks (master -> origin)
λ git stashSaved working directory and index state WIP on master: 542a055 create .gitignore
D:\learning\git\timed_tasks (master -> origin)
λ git stash clearD:\learning\git\timed_tasks (master -> origin)
λ git statusOn branch masterYour branch is up to date with 'origin/master'.
nothing to commit, working tree clean