这里直接贴一个参考合集,该合集内容较多,这里只提取关键部分
从零开始手把手学习 Git 神器_慧天城寻的博客-CSDN博客
基础操作
创建分支
1 | # 1. 创建分支 |
拉取远端指定分支
1 | //查看远程分支 |
查看当前修改了哪些内容
<font style="color:rgb(199, 37, 78);background-color:rgb(249, 242, 244);">git status</font> 命令⽤于查看在你上次提交之后是否有对⽂件进⾏再次修改。
1 | git status |
<font style="color:rgb(199, 37, 78);background-color:rgb(249, 242, 244);">git diff [file]</font> 命令⽤来显⽰暂存区和⼯作区⽂件的差异,显⽰的格式正是Unix通⽤的diff格式。也可以使⽤ <font style="color:rgb(199, 37, 78);background-color:rgb(249, 242, 244);">git diff HEAD -- [file]</font> 命令来查看版本库和⼯作区⽂件的区别。
注意:如果是新增的文件,git diff不会显示,但在git status会有
撤销修改
1 | # 将文件回退到上一次add或者commit的状态 |
版本回退
将工作区文件回到上一次提交时的状态,也即从版本库重新拉取文件到工作区。
1 | # 将文件回到上一次提交状态(add和commit都算) |
执⾏ git reset 命令⽤于回退版本,可以指定退回某⼀次提交的版本。要解释⼀下“回退”本质是要将版本库中的内容进⾏回退,⼯作区或暂存区是否回退由命令参数决定:
git reset 命令语法格式为:git reset [–soft | –mixed | –hard] [HEAD]
–mixed 为默认选项,使⽤时可以不⽤带该参数。该参数将暂存区的内容退回为指定提交版本内容,⼯作区⽂件保持不变。
–soft 参数对于⼯作区和暂存区的内容都不变,只是将版本库回退到某个指定版本。
–hard 参数将暂存区与⼯作区都退回到指定版本。切记⼯作区有未提交的代码时不要⽤这个命令,因为⼯作区会回滚,你没有提交的代码就再也找不回了,所以使⽤该参数前⼀定要慎重。
HEAD 说明:
可直接写成 commit id,表⽰指定退回的版本
HEAD 表⽰当前版本
HEAD^ 表示上⼀个版本
HEAD^^ 表示上上⼀个版本
以此类推…
也可以使⽤ ~数字 表⽰:
HEAD~0 表⽰当前版本
HEAD~1 上⼀个版本
HEAD~2 上上⼀个版本
以此类推…
提交到暂存区
1 | git add . |
提交到仓库
1 | git commit -m "message" |
git log和git relog
1 | git log |
分支管理
查看,新建,切换分支
1 | git branch |
合并分支
<font style="color:rgb(199, 37, 78);background-color:rgb(249, 242, 244);">git merge</font> 命令⽤于合并指定分⽀到当前分⽀。
<font style="color:rgb(199, 37, 78);background-color:rgb(249, 242, 244);">Fast-forward</font> 代表“快进模式”,也就是直接把 master 指向 dev 的当前提交,所以合并速度⾮常快。
当然,也不是每次合并都能 <font style="color:rgb(199, 37, 78);background-color:rgb(249, 242, 244);">Fast-forward</font>,我们后⾯会讲其他⽅式的合并。
Git ⽀持我们强制禁⽤ <font style="color:rgb(199, 37, 78);background-color:rgb(249, 242, 244);">Fast forward</font> 模式,那么就会在 merge 时⽣成⼀个新的 <font style="color:rgb(199, 37, 78);background-color:rgb(249, 242, 244);">commit</font> ,这样,从分⽀历史上就可以看出分⽀信息。
下⾯我们实战⼀下 <font style="color:rgb(199, 37, 78);background-color:rgb(249, 242, 244);">--no-ff</font> ⽅式的 <font style="color:rgb(199, 37, 78);background-color:rgb(249, 242, 244);">git merge</font> 。
请注意 <font style="color:rgb(199, 37, 78);background-color:rgb(249, 242, 244);">--no-ff</font> 参数,表⽰禁⽤ <font style="color:rgb(199, 37, 78);background-color:rgb(249, 242, 244);">Fast forward</font> 模式。禁⽤ <font style="color:rgb(199, 37, 78);background-color:rgb(249, 242, 244);">Fast forward</font> 模式后合并会创建⼀个新的 <font style="color:rgb(199, 37, 78);background-color:rgb(249, 242, 244);">commit</font> ,所以加上 <font style="color:rgb(199, 37, 78);background-color:rgb(249, 242, 244);">-m</font> 参数,把描述写进去。
1 | git merge [branch_name] |
如果你先在master上新建了一个分支branch1,然后在branch1上进行修改,这个时候你想丢弃修改,试图使用git merge master将master分支合并到当前分支,结果发现毫无变化。
注意merge并不是将master分支的内容复制到当前分支,只是把master上的提交合入当前分支的提交树,这两个分支上的所有提交的历史都是存在的。由于branch1是从master分支创建的,且之后master没有改变,那么执行merge不会有任何变化。
删除分支
合并完成后, dev 分⽀对于我们来说就没⽤了, 那么 dev 分⽀就可以被删除掉,使用 <font style="color:rgb(199, 37, 78);background-color:rgb(249, 242, 244);">git branch -D</font> 命令,注意如果当前正处于某分⽀下,就不能删除当前分⽀。需要切换到其他分支进行删除。
保存工作区状态
如果需要暂时保存工作区内容,切换分支可以如下操作
1 | # 暂时存储工作区内容 |
恢复现场也可以采⽤ <font style="color:rgb(199, 37, 78);background-color:rgb(249, 242, 244);">git stash apply</font> 恢复,但是恢复后,stash内容并不删除,你需要⽤ <font style="color:rgb(199, 37, 78);background-color:rgb(249, 242, 244);">git stash drop</font> 来删除;
你也可以进行多次 stash,恢复的时候,先⽤ <font style="color:rgb(199, 37, 78);background-color:rgb(249, 242, 244);">git stash list</font> 查看,然后恢复指定的 stash,⽤命令 <font style="color:rgb(199, 37, 78);background-color:rgb(249, 242, 244);">git stash apply stash@{0}</font> , 这部分请读者⾃⾏查看使⽤。
正确的合并方式
最好在⾃⼰的分⽀上合并下 <font style="color:rgb(199, 37, 78);background-color:rgb(249, 242, 244);">master</font> ,再让 <font style="color:rgb(199, 37, 78);background-color:rgb(249, 242, 244);">master</font> 去合并 <font style="color:rgb(199, 37, 78);background-color:rgb(249, 242, 244);">dev</font> ,这样做的⽬的是有冲突可以在本地分⽀解决并进⾏测试,⽽不影响 <font style="color:rgb(199, 37, 78);background-color:rgb(249, 242, 244);">master</font>
远程仓库
1 | git remote |
1 | git push <远程主机名> <本地分⽀名>:<远程分⽀名> |
1 | git pull <远程主机名> <远程分⽀名>:<本地分⽀名> |
远端拉取指定分支到本地
1 | # 查看远端分支 |
设置代理
1 | # 查看 |