基础命令 git status
查看工作区状态,如果跟踪的文件有做任何修改,都可以通过该命令来发现。 如:这里通过git status就发现在develop分支上,README.md文件发生了更改。
jere@JereMBP GitTest (develop) $ git status
On branch develop
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: README.md
no changes added to commit (use "git add" and/or "git commit -a")
jere@JereMBP GitTest (develop) $ git status
On branch develop
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: README.md
modified: dev-file.txt
no changes added to commit (use "git add" and/or "git commit -a")
jere@JereMBP GitTest (develop) $ git checkout README.md
Updated 1 path from the index
jere@JereMBP GitTest (develop) $ git status
On branch develop
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: dev-file.txt
no changes added to commit (use "git add" and/or "git commit -a")
jere@JereMBP GitTest (develop) $
jere@JereMBP GitTest (develop) $ git status
On branch develop
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: README.md
no changes added to commit (use "git add" and/or "git commit -a")
jere@JereMBP GitTest (develop) $ git add README.md
jere@JereMBP GitTest (develop) $ git status
On branch develop
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: README.md
jere@JereMBP GitTest (develop) $
jere@JereMBP GitTest (develop) $ git checkout main
Switched to branch 'main'
Your branch is behind 'origin/main' by 1 commit, and can be fast-forwarded.
(use "git pull" to update your local branch)
jere@JereMBP GitTest (main) $ git merge origin/main
Updating 30f049e..d6ff31d
Fast-forward
README.md | 2 ++
1 file changed, 2 insertions(+)
jere@JereMBP GitTest (main) $
jere@JereMBP GitTest (main) $ git merge develop
Auto-merging README.md
CONFLICT (content): Merge conflict in README.md
Automatic merge failed; fix conflicts and then commit the result.
jere@JereMBP GitTest (main) $ git status
On branch main
Your branch is up to date with 'origin/main'.
You have unmerged paths.
(fix conflicts and run "git commit")
(use "git merge --abort" to abort the merge)
Changes to be committed:
new file: dev-file.txt
Unmerged paths:
(use "git add <file>..." to mark resolution)
both modified: README.md
jere@JereMBP GitTest (main) $
正是README.md文件发生了冲突,我们需要打开该文件来查看具体冲突内容。
# GitTest
For git command practice
<<<<<<< HEAD
chagne README file on the main branch //内容1
=======
do something on develop branch //内容2
>>>>>>> develop
jere@JereMBP GitTest (main) $ vim README.md
jere@JereMBP GitTest (main) $ git status
On branch main
Your branch is up to date with 'origin/main'.
You have unmerged paths.
(fix conflicts and run "git commit")
(use "git merge --abort" to abort the merge)
Changes to be committed:
new file: dev-file.txt
Unmerged paths:
(use "git add <file>..." to mark resolution)
both modified: README.md
jere@JereMBP GitTest (main) $ git commit -am "解决README冲突"
[main 8e08c23] 解决README冲突
jere@JereMBP GitTest (main) $
此时feature-2利用 git rebase origin develop 来同步feature-1的代码。
jere@JereMBP GitTest (feature-2) $ git rebase origin/develop
First, rewinding head to replay your work on top of it...
Applying: 添加 feature-22.txt 文件
Applying: 修改feature-22.txt
jere@JereMBP GitTest (feature-2) $
这三个状态的节点分支情况如下图所示:
初始状态feature-1合并到developfeature-2同步develop代码
整理合并提交
有时候,我们想对自己的提交进行合并操作。
我们可以通过git rebase -i HEAD~x 来合并(这里 i 的意思为interactive交互,HEAD~x代表要合并HEAD到前x个历史提交,如: HEAD~2为历史的前两个提交,HEAD~4就是历史的前四个提交)。
举个例子: 我们在feature-3分支上对feature-3.txt进行了两次提交修改,分别是598cc68 修改feature-3.txt与8561ef3 再次修改feature-3.txt,现在我们要将这两个提交进行合并。
pick 598cc68 修改feature-3.txt
s 8561ef3 再次修改feature-3.txt
...省略...
保存退出,将自动打开另外一个vim文件,用来修改提交文本信息,如下:
# This is a combination of 2 commits.
# This is the 1st commit message:
修改feature-3.txt
# This is the commit message #2:
再次修改feature-3.txt
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
这里我们将其修改为最终版本,修改feature-3.txt,保存退出。
jere@JereMBP GitTest (feature-3) $ git rebase -i HEAD~2
[detached HEAD 18296c5] 最终版本,修改feature-3.txt
Date: Wed Dec 1 10:21:05 2021 +0800
1 file changed, 2 insertions(+)
Successfully rebased and updated refs/heads/feature-3.
jere@JereMBP GitTest (feature-3) $
此时,你本地分支已经完成了合并,你尝试将其推送到仓库中时,会发现被拒绝。通过提示可知,拒绝理由是our current branch is behind 你当前分支落后远程分支,这时,我们需要 git push origin feature-3 -f,强制推送,完成合并。
jere@JereMBP GitTest (feature-3) $ git push origin feature-3
To https://github.com/JereChen11/GitTest.git
! [rejected] feature-3 -> feature-3 (non-fast-forward)
error: failed to push some refs to 'https://github.com/JereChen11/GitTest.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
jere@JereMBP GitTest (feature-3) $ git push origin feature-3 -f
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 12 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 356 bytes | 356.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/JereChen11/GitTest.git
+ 8561ef3...18296c5 feature-3 -> feature-3 (forced update)
这三个状态的节点分支情况如下图所示:
执行 git rebase -i HEAD~2采用squash合并提交,并修改文本信息强制推送到仓库
还记得我们将 pick 改为 s 这一步吗?这里的s指的是squash,意思是将该提交挤压合并到上一个提交。 他还有一些其它选项操作可以了解一下:
jere@JereMBP GitTest (develop) $ git co main
Switched to branch 'main'
Your branch is up to date with 'origin/main'.
jere@JereMBP GitTest (main) $ git cherry-pick d818f10
[main 55e808c] 紧急修复线上bug
Date: Wed Dec 1 15:49:48 2021 +0800
1 file changed, 2 insertions(+)
jere@JereMBP GitTest (feature-5) $ git status
On branch feature-5
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: feature-5.txt
no changes added to commit (use "git add" and/or "git commit -a")
jere@JereMBP GitTest (feature-5) $ git stash
Saved working directory and index state WIP on feature-5: 9bd4e1f 添加 feature-5.txt
jere@JereMBP GitTest (feature-5) $ git status
On branch feature-5
nothing to commit, working tree clean
jere@JereMBP GitTest (feature-5) $ git stash pop
On branch feature-5
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: feature-5.txt
no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (1ec48d00e8d1bd5c0042d88d1209dbb9051815d4)
jere@JereMBP GitTest (feature-5) $
另外,git stash 还有一些常用操作。
# 保存到stash栈中,并加上自定义message修饰
$ git stash save "message"
# 列出stash栈中所有元素
$ git stash list
# 应用stash栈中的第x个元素,pop是应用的同时且从栈中删除,而apply则是只应用不删除
$ git stash apply stash@{x}
# 删除stash栈中的第x个元素
$ git stash drop stash@{x}
上线发版啦
当我们产品开发完成,发现上线时,我们需要打个标签来标注一下,以便下次更好的找它。
这时我们就可以通过git tag 标签名来打标签。
举个例子:我们的1.0版本上线了,所以打个v1.0。
jere@JereMBP GitTest (feature-5) $ git tag v1.0
jere@JereMBP GitTest (feature-5) $ git tag
v1.0
v1.0.1