Skip to content

学习如何使用git

©️ Copyright 2024 @ Authors
Author: Trinity 📨
Date:2024-09-08
Need more help? See more information in My GitHub Homepage or send me email. Thanks!


学习资料源自廖雪峰老师博客Git教程

目录

1. 创建版本库

将目录变为Git可管理的仓库:

git init
输出:
Initialized empty Git repository in /Users/michael/learngit/.git/
目录下多一个.git目录,没事别找他麻烦。

2. 添加一个文件到仓库

第一步 添加文件

编写一个README.md文件,注意要放到.git同目录下,使用命令git add添加到仓库:

git add README.md
没有输出?这就对了。没有就是好消息。

第二步 提交更改至仓库

使用命令git commit将文件提交至仓库

git commit -m "add readme file"

3. 修改文件

第一步 修改文件

向README.md文件中添加:

Git is a version control system.

第二步 查看仓库状态

使用命令git status查看modified,输出为:

On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   README.md

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        Study_Git/

no changes added to commit (use "git add" and/or "git commit -a")
这样我们可以实时掌控仓库的状态,监控什么被修改过,但是还没有准备提交。

第三步 查看修改内容

命令git status只能查看修改了什么文件,但是看不出修改内容,使用命令git diff查看。

git diff README.md
输出:
diff --git a/README.md b/README.md
index 8df08ff..b8b3205 100644
--- a/README.md
+++ b/README.md
@@ -2,3 +2,17 @@
 Welcome to my  persional Homepage!
 ## Let‘s strart together!
 **The webpage is currently under construction**
+
+<div style="color:white; background-color:#354538; border: 1px solid #FFE0C3; border-radius: 10px; margin-bottom:0rem">
+    <p style="margin:1rem; padding-left: 1rem; line-height: 2.5;">
+        ©️ <b><i>Copyright 2023 @ Authors</i></b><br/>
+        <i>Author:
+            <b>
+            <a href="mailto:qhyu@stu.suda.edu.com">Trinity 📨 </a>
+            </b>
+        </i>
+        <br/>
+        <i>Date:2023-09-11</i><br/>
+        <i>Need more help? </a>See more information in <a rel="license" href="https://github.com/Ternity/Trinity.github.io/">My GitHub Homepage </a>or send me email. Thanks!</i><br/>
+    </p>
+</div>
\ No newline at end of file
这里的diff表示的是本地磁盘上的内容和本地git分支上内容的差异。

第四步 添加至暂存区

当确认git diff的自己修改的内容无误后,可以使用命令git add将修改的文件添加至暂存区:

git add READMD.md
同样没有任何输出,使用命令git status:
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   README.md

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        Study_Git/
git告诉我们将要被提交的修改包括README.md文件,那我们就可以放心提交修改了:
git commit -m "add distributed"
输出提示:
[master fc7c02d] add distributed
 1 file changed, 14 insertions(+)
提交后再用git status命令查看仓库状态: 提示:
On branch master
nothing to commit, working tree clean
可以看到git表示当前没有在暂存区的文件,工作区是干净的。

4. 同步到远程仓库

使用命令提交至远程仓库:

# 1.首先需要添加远程仓库
git remote add origin https://github.com/Ternity/Ternity.github.io/tree/master
# 2.如果网络有问题,可以使用SSH而不是HTTPS
git remote set-url origin git@github.com:Ternity/Ternity.github.io.git
# 3.将master分支推送到远程仓库
git push origin master
# 4.也可以将所有分支和标签推送到远程仓库
git push new-origin --all
git push new-origin --tags
# 如果需要添加到新远程仓库
git remote add new-origin <新仓库的URL>
# 然后执行上面2-4,然后有需要可以删除旧的远程仓库,并重命名新仓库
git remote remove origin
git remote rename new-origin origin
当我们提交至remote 仓库时候,输出:
Counting objects: 18, done.
Delta compression using up to 12 threads.
Compressing objects: 100% (17/17), done.
Writing objects: 100% (18/18), 198.42 KiB | 796.00 KiB/s, done.
Total 18 (delta 3), reused 0 (delta 0)
remote: Resolving deltas: 100% (3/3), done.
remote:
remote: Create a pull request for 'master' on GitHub by visiting:
remote:      https://github.com/Ternity/Trinity.github.io/pull/new/master
remote:
To github.com:Ternity/Trinity.github.io.git
 * [new branch]      master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.

5. 将本地master分支推送到远程main分支

本地master推送到远程main分支

git push origin master:main
如果是想将本地master分支重命名为main分支并推送到master,则
git branch -m master main
git push origin main

6. 多分支协作流程

创建新分支-提交远端-同步远端-合并修改-删除分支

6.1 创建新分支

无论是clone别人的代码还是自己本地的main(master)分支的代码,直接在main分支上修改是不良行为。一方面,对于多人协作仓库,创建新的分 支可以区分不同人的工作;另一方面,新的PR(pull request)也不会使之前的main的code失效。因此我们可以使用如下命令从原有的main分支复制一个新的分支,并重命名为dev分支:

git checkout -b dev
现在,我们有两个分支,maindev,使用如下命令命令查看当前有哪些分支:
git branch
可以发现现在本地git中有两个git分支,maindev,其中*表示当前所在的分支:
* dev
  main
我们可以在dev分支上修改,添加修改文件到暂存区(git add xxx),确认无误(git diff),提交修改到本地仓库(git commit -m "xxx")。

6.2 提交远端

将本地git的dev分支推送到远程仓库(git push origin dev)。这样就不会影响到远程仓库的main分支的代码。

6.3 同步远端

6.3 同步远端

一种很常见的情况是,远程仓库中的main分支有了更新update,而我们的dev分支相对于init代码也有了更新feature,这时候我们需要测试我们的feature能否在main分支更新的update下工作。这需要将远程仓库的main分支的update更新同步到本地仓库的dev分支。

6.3.1 远程仓库main分支最新的update同步到本地仓库的main分支

首先,我们需要切换当前分支devmain分支:

git checkout main
将远程仓库的main分支更新同步到本地仓库的main分支
git pull origin main
这样我们本地git分支main就获得了远程仓库的main分支的update

6.3.2 将本地仓库的main分支的update同步到dev分支

接下来我回到dev分支,并将main分支的更新同步到dev分支:

git checkout dev
git rebase main   
git rebase main这个命令的作用:
之前devfeature先不管,将main的修改同步到dev,然后在此基础上将feature尝试合并。 然后再将本地仓库的main分支合并到本地仓库的dev分支。

6.3.3 解决冲突

上面这个过程可能会有 rebase conflict,需要手动选择冲突代码。 上一步的git rebase main命令会提示冲突的文件,也可使用git status命令查看冲突文件。

warning: squelched 2140 whitespace errors
warning: 2145 lines add whitespace errors.
Falling back to patching base and 3-way merge...
Auto-merging docs/index.md
CONFLICT (add/add): Merge conflict in docs/index.md
error: Failed to merge in the changes.
Patch failed at 0002 create web use mkdocs-material
Use 'git am --show-current-patch' to see the failed patch

Resolve all conflicts manually, mark them as resolved with
"git add/rm <conflicted_files>", then run "git rebase --continue".
You can instead skip this commit: run "git rebase --skip".
To abort and get back to the state before "git rebase", run "git rebase --abort".
找到相应的文件,这里是docs/index.md,打开文件分析选择需要保留的部分。
使用git add命令将冲突文件标记为已解决,然后使用git rebase --continue继续rebase。
git add docs/index.md
git rebase --continue
有时候continue之后,git会提醒你还有哪些冲突需要合并。重复上述步骤,直到rebase完成。这时候continue会提醒你是否忘记使用add?这基本意味着rebase完成,continue的输出如下:
Applying: change file docs/index.md
No changes - did you forget to use 'git add'?
If there is nothing left to stage, chances are that something else
already introduced the same changes; you might want to skip this patch.

Resolve all conflicts manually, mark them as resolved with
"git add/rm <conflicted_files>", then run "git rebase --continue".
You can instead skip this commit: run "git rebase --skip".
To abort and get back to the state before "git rebase", run "git rebase --abort".
这时候使用git branch查看当前分支,会发现正处于一个叫no branch的分支:
* (no branch, rebasing mkdocs)
  dev
  main
  mkdocs
使用git status会发现同样处于rebase状态:
rebase in progress; onto 73c2da0
You are currently rebasing branch 'mkdocs' on '73c2da0'.
  (all conflicts fixed: run "git rebase --continue")

nothing to commit, working tree clean
然后我们就可以使用git rebase --skip完成这次rebase了。使用git branch发现已经不在rebase状态了。

6.4 合并修改

更新完dev分支后,我们可以将本地dev分支更新到远程仓库的dev分支:

git push -f origin dev    # 由于rebase,需要强制推送
一切准备就绪,我们可以将GitHub的dev版本的代码合并到main分支,此事即为Pull Request(PR)
这里需要在GitHub上操作。
Commpare&pull_request
在确认New pull request后,代码管理员有三个选项可选:
merge
Merge pull request:所有添加到dev分支的commit都会被添加到main分支,由于commit可能很多,不推荐。
Squash and merge:将dev分支的所有commit合并为一个commit,然后再添加到main分支,推荐。
Rebase and merge:将dev分支的所有commit合并为一个commit,并rebasemain分支。

我们希望main分支里的commit尽可能简洁且代码可正常工作,因此推荐代码管理员选择Squash and merge操作,形成新的commituodate2

6.5 删除分支

dev分支的代码合并到main分支后,dev分支就没有存在的必要了,可以删除。
GitHub上有操作为Delete branch。这样远程仓库的dev分支就被删除了。
Delete branch
然后本地的dev分支也可以删除:

git checkout main   # 本地分支切换到main
git branch -D dev   # 删除dev分支
将远程main分支最新更新update2同步到本地:
git pull origin main
这时候,远程、本地git的main分支内容就一致啦!