git实用指南

这里直接贴一个参考合集,该合集内容较多,这里只提取关键部分

从零开始手把手学习 Git 神器_慧天城寻的博客-CSDN博客

基础操作

创建分支

1
2
3
4
# 1. 创建分支

git branch firstBranch
git branch -b firstBranch # 创建并切换到分支

拉取远端指定分支

1
2
3
4
5
6
7
8
9
//查看远程分支
git branch -r
//创建本地分支并关联
git checkout -b 本地分支 origin/远程分支

//已有本地分支创建关联
git branch --set-upstream-to origin/远程分支名 本地分支名
//拉取
git pull

查看当前修改了哪些内容

<font style="color:rgb(199, 37, 78);background-color:rgb(249, 242, 244);">git status</font> 命令⽤于查看在你上次提交之后是否有对⽂件进⾏再次修改。

1
2
git status
git diff [file]

<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
2
3
4
5
6
7
8
9
10
# 将文件回退到上一次add或者commit的状态
git checkout -- [file]

# 文件已经add了
# 将暂存区回退,工作区不变
git reset --mixed HEAD [file]
git checkout -- [file]

# 文件已经commit
git reset --hard HEAD^ [file]

版本回退

将工作区文件回到上一次提交时的状态,也即从版本库重新拉取文件到工作区。

1
2
3
4
5
6
7
# 将文件回到上一次提交状态(add和commit都算)
git checkout -- [file]

git reset --hard [版本号]

#本地回退之后强制推送到远端
git push --force

执⾏ 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
2
3
4
5
6
git log
git log --pretty=oneline
git log --pretty=oneline --abbrev-commit -<number>
git log --pretty=format:"%h - %an %cr : %s" --graph -<number>

git relog

分支管理

查看,新建,切换分支

1
2
3
4
5
6
7
8
git branch
git branch -r

git branch [new_branch]

git checkout -b [new_branch]

git checkout [branch_name]

合并分支

<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
2
git merge [branch_name]
git merge --no-ff -m "message" [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
2
3
4
5
6
7
8
9
10
11
# 暂时存储工作区内容
git stash

# 显示stash内容
git stash list

# 恢复之前工作区内容
git stash pop

git stash apply
git stash drop

恢复现场也可以采⽤ <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
2
3
git remote

git remote -v
1
2
3
4
5
git push <远程主机名> <本地分⽀名>:<远程分⽀名>

# 如果本地分⽀名与远程分⽀名相同,则可以省略冒号:
git push <远程主机名> <本地分⽀名>

1
2
3
4
5
git pull <远程主机名> <远程分⽀名>:<本地分⽀名>

# 如果远程分⽀是与当前分⽀合并,则冒号后⾯的部分可以省略。
git pull <远程主机名> <远程分⽀名>

远端拉取指定分支到本地

1
2
3
4
5
6
7
8
9
# 查看远端分支
git ls-remote --heads origin

# 添加远端分支到本地
git branch --track branch1 origin/branch1

# 拉取并合并分支
git checkout branch1
git pull origin branch1

设置代理

1
2
3
4
5
6
7
8
9
10
11
12
# 查看
git config --global --get http.proxy
git config --global --get https.proxy

# 设置
git config --global http.proxy http://127.0.0.1:7890
git config --global https.proxy http://127.0.0.1:7890

# 取消代理设置
git config --global --unset http.proxy
git config --global --unset https.proxy