码场新人如何使用Git

166 阅读6分钟

码场新人如何使用Git

本文涉及的命令摘要:分支选择,代码提交,代码暂存,远程追踪,代码重置,代码的冲突解决,代码开发前后对比。

1) 简要说明

git代码仓库 可分为本地库和远程库,同理branch也分为本地分支和远程分支。以idea为例,在右下角中可以看到该项目的git分支branch情况,origin即为远程分支。

1607358666249.png

2) 新建/切换分支

一般来说 master会作为项目的主分支投入生产环境,而其余分支各有其用,如测试环境的分支testing,预发分支preview等,对于开发者来说 我们需要建立一个自己的分支。

2-1 git checkout -b jam
2-2 git checkout jam

命令2-1可以用来创建新的分支,命令2-2可以用来切换到已有的分支。

git checkout -b jam Switched to a new branch 'jam'

3) 代码拉取/远程追踪

3-1 git pull
3-2 git pull --rebase
3-3 git branch --set-upstream-to=origin/<branch> <branch>

命令3-1 与 命令3-2 都可以拉取远程分支的最新代码。

命令3-3用来设置追踪的远程分支(基分支)。

新建了自己的分支,需为自己的代码设置追踪分支,简单描述一下追踪的含义,个人理解举例:代码分支里有master分支,a分支,a分支为你的开发分支,master为主分支,在master分支的基础上新建了a分支经过了一段时间后,master分支发生了变化。

1607447180327.png

示例:git branch --set-upstream-to=origin/master a

设置a分支追踪origin/master分支,当远端库的master发生改变时,通过命令3-1或3-2拉取最新的代码。不同的是 git pull相当于git fetch + git merge 而 git pull --rebase 相当于 git fetch + git rebase ,rebase执行了一次变基操作,(以上图为例,原本a的基为第二个master version执行后变基到第三个master version)

tips:如果写了一部分代码但还未提交,pull --rebase 会告诉你将代码提交会暂存,可通过 git stash 将本地代码暂存,执行完后用 git stash pop 将暂存取出

4) 代码提交

4-1 git add . 
4-2 git commit -m "description"
4-3 git push origin jam
4-4 git push -f origin jam

在切换到本地自己的开发分支后,完成自己的开发工作后,通过命令4-1和4-2提交到本地的代码库里,本地仓库只要不往远端push随便搞,提交到本地的代码库后,下一步就是往远端的git仓库推送自己的分支,一般还是以自己的名字等作为分支推,push命令见4-3和4-4,4-4中的 f 为force 代表强制覆盖,当不止一次的想远端push代码时 往往会出现代码冲突的情况(本地为最新代码),通过 -f 可以直接用本地覆盖强行覆盖。

5) 代码重置与rebase

5-1 git log
5-2 git reset <version>
5-3 git rebase -i 

通过命令5-1可以看到代码的提交记录,

commit d929101a8375a18fcd0dce336eed8a7a5a9357f6 (HEAD -> jam)
Author: Jam <***>
Date:   Wed Dec 9 01:16:59 2020 +0800

    test2

commit d41af8994d568254bb83d91c0a8fc89f267ac0ec
Author: Jam <***>
Date:   Tue Dec 8 01:04:25 2020 +0800

    test

log信息里的 commit 后面所带的一串序列号,为git 版本号是版本的唯一标识符,最下面那行的 test2 和 test为提交的描述。

当写完一段代码commit之后想要撤销commit操作,将代码重置到commit之前(这里的重置是指版本重置到编码前,但编码的内容会以未提交的代码形式依然留存),简而言之就是撤回commit这一步 ,见命令5-2。如想撤销 test2 这个commit,则 git reset d41af8994d568254bb83d91c0a8fc89f267ac0ec

命令5-3一般是拿来做合并多个提交的,当然 5-2也可以做这样的事情,我们在进行编码开发的时候 难免会出现多次commit的情况,但当我们要合并到master分支的时候 一般都会要求合并为一个commit 。两个方案,一个是通过 git reset回到最初的version,再commit一次,另一个方法就是通过 git rebase -i 。

git reabase -i

pick d41af89 test
pick d929101 test2

# Rebase bf863dd..d929101 onto bf863dd (2 commands)
#
# Commands:
# p, pick <commit> = use commit
# r, reword <commit> = use commit, but edit the commit message
# e, edit <commit> = use commit, but stop for amending
# s, squash <commit> = use commit, but meld into previous commit
# f, fixup <commit> = like "squash", but discard this commit's log message
# x, exec <command> = run command (the rest of the line) using shell
# b, break = stop here (continue rebase later with 'git rebase --continue')
# d, drop <commit> = remove commit
# l, label <label> = label current HEAD with a name
# t, reset <label> = reset HEAD to a label
# m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
# .       create a merge commit using the original merge commit's
# .       message (or the oneline, if no original merge commit was
# .       specified). Use -c <commit> to reword the commit message.
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out

执行命令后会进入vim界面 出现上面这一坨东西,可以看到此时有两个commit test 和test2 ,下面是一些指导Commands,而我们想合并这两个commit 只需要用到 pick 和 squash即可。

pick d41af89 test
s d929101 test2

修改为如上,继续各种保存下一步,即可将这两个commit合并为一个。

6) 代码冲突解决

代码冲突相比上面来说不难理解,但是操作起来需要相对谨慎,代码冲突对于第一次使用git协作开发的人简直是噩梦。

Auto-merging src/java/cn/Test.java
CONFLICT (content): Merge conflict in src/java/cn/Test.java
Automatic merge failed; fix conflicts and then commit the result.

此时可以通过右键整个工程 Git -> Resovle Conflicts来手动解决冲突。

image.png

出现以下界面。此时有三种选择 Accept Yours为直接用当前分支(Jam) 的代码直接覆盖,Accept Theirs指的是被合并的那个分支(master)直接覆盖即丢弃你分支下的所有改动,一般来说都不会选择这两种粗暴的模式,而是通过Merge来手动的决定保留哪些代码

image.png 点击merge后会出现以下界面。两边是两个分支现有的代码,而中间Resutl是合并后的结果代码。根据你的实际情况决定哪个分支的代码块保留,通过点击 >> 或者 << ,以及魔法棒,按实际情况选择。

image.png 完成后点击Apply,此时并还没有完全解决。看上述的命令行 Automatic merge failed; fix conflicts and then commit the result. 此时只是解决掉了冲突,但是自动合并失败掉了,会发现又下角的分支情况 处于 Merging jam的状态,需要再键入一个命令 git merge --continue 。

如果在冲突出现后想终止掉合并流程(即回到执行 git pull之前的状态 ),可以键入 git merge --abort