找回密码
 立即注册
首页 业界区 安全 Git基础操作及协作流程

Git基础操作及协作流程

赙浦 2025-9-29 14:00:15
一整套流程帮你实践整个 Git 操作基础及协作流程。
来源:https://docs.microsoft.com/zh-cn/learn/paths/intro-to-vc-git/
Git 介绍

配置 Git

确认已经安装 git
  1. git --version
复制代码
输出
  1. git version 2.30.1 (Apple Git-130)
复制代码
配置 Git,必须定义一些全局变量:user.name 和 user.email。
  1. git config --global user.name "<USER_NAME>"
  2. git config --global user.email "<USER_EMAIL>"
复制代码
检查更改是否成功。
  1. git config --list
复制代码
输出
  1. user.name=User Name
  2. user.email=user-name@contoso.com
复制代码
配置 Git 存储库

创建名为“Cats”的文件夹,此文件夹为项目目录(也称为“工作树”)
  1. mkdir Cats
  2. cd Cats
复制代码
初始化新存储库,将默认分支名称设置为 main:
  1. git init --initial-branch=main
  2. git init -b main
复制代码
使用 git status 显示工作树的状态:
  1. git status
复制代码
输出
  1. On branch main
  2. No commits yet
  3. nothing to commit (create/copy files and use "git add" to track)
复制代码
使用 ls -a 显示工作树的内容:
  1. ls -a
复制代码
确认目录包含一个名为.git 的子目录。此文件夹为 Git 存储库——用于存储工作树的元数据和历史记录的目录。
从 Git 获取帮助
  1. git --help
复制代码
输出
  1. usage: git [--version] [--help] [-C <path>] [-c <name>=<value>]
  2.            [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
  3.            [-p | --paginate | -P | --no-pager] [--no-replace-objects] [--bare]
  4.            [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
  5.            <command> []
  6. These are common Git commands used in various situations:
  7. start a working area (see also: git help tutorial)
  8.    clone             Clone a repository into a new directory
  9.    init              Create an empty Git repository or reinitialize an existing one
  10. work on the current change (see also: git help everyday)
  11.    add               Add file contents to the index
  12.    mv                Move or rename a file, a directory, or a symlink
  13.    restore           Restore working tree files
  14.    rm                Remove files from the working tree and from the index
  15.    sparse-checkout   Initialize and modify the sparse-checkout
  16. examine the history and state (see also: git help revisions)
  17.    bisect            Use binary search to find the commit that introduced a bug
  18.    diff              Show changes between commits, commit and working tree, etc
  19.    grep              Print lines matching a pattern
  20.    log               Show commit logs
  21.    show              Show various types of objects
  22.    status            Show the working tree status
  23. grow, mark and tweak your common history
  24.    branch            List, create, or delete branches
  25.    commit            Record changes to the repository
  26.    merge             Join two or more development histories together
  27.    rebase            Reapply commits on top of another base tip
  28.    reset             Reset current HEAD to the specified state
  29.    switch            Switch branches
  30.    tag               Create, list, delete or verify a tag object signed with GPG
  31. collaborate (see also: git help workflows)
  32.    fetch             Download objects and refs from another repository
  33.    pull              Fetch from and integrate with another repository or a local branch
  34.    push              Update remote refs along with associated objects
  35. 'git help -a' and 'git help -g' list available subcommands and some
  36. concept guides. See 'git help <command>' or 'git help <concept>'
  37. to read about a specific subcommand or concept.
  38. See 'git help git' for an overview of the system.
复制代码
创建和修改 Git 项目

创建和添加(暂存)文件

使用 touch创建名为 index.html 的文件
  1. touch index.html
复制代码
如果文件存在,touch 会更新文件的上次修改时间。
使用 git status 获取工作树的状态:
  1. git status
复制代码
输出:
  1. On branch main
  2. No commits yet
  3. Untracked files:
  4.   (use "git add <file>..." to include in what will be committed)
  5.   index.html
  6. nothing added to commit but untracked files present (use "git add" to track)
复制代码
使用 git add 将新文件添加到 Git 的“index”,然后是用 git status 检查状态。. 表示为当前目录中的所有文件添加编制索引。
  1. git add .
复制代码
使用 git status 以确保更改已暂存无误:
  1. git status
复制代码
输出
  1. On branch main
  2. No commits yet
  3. Changes to be committed:
  4.   (use "git rm --cached <file>..." to unstage)
  5.   new file:   index.html
复制代码
进行首次提交

使用以下命令创建新提交
  1. git commit index.html -m "Create an empty index.html file"
复制代码
输出
  1. [main (root-commit) 166b2d9] Create an empty index.html file
  2. 1 file changed, 0 insertions(+), 0 deletions(-)
  3. create mode 100644 index.html
复制代码
使用 git log 显式有关提交的信息:
  1. git log
复制代码
输出
  1. commit 166b2d94a49ecbe6008aa027e9d2f7d870c78724 (HEAD -> main)
  2. Author: duzhida <entercoder1993@gmail.com>
  3. Date:   Fri Feb 25 19:54:08 2022 +0800
  4.     Create an empty index.html file
复制代码
修改 index.html 并提交更改

修改 index.html,将以下语句粘贴到编辑器。
  1. <h1>Our Feline Friends</h1>
复制代码
使用 git status 检查工作树状态:
  1. git status
复制代码
输出
  1. On branch main
  2. Changes not staged for commit:
  3.   (use "git add <file>..." to update what will be committed)
  4.   (use "git restore <file>..." to discard changes in working directory)
  5.   modified:   index.html
  6. no changes added to commit (use "git add" and/or "git commit -a")
复制代码
提交更改
  1. git commit -a -m "Add a heading to index.html"
复制代码
输出
  1. [main 57cdf5c] Add a heading to index.html
  2. 1 file changed, 1 insertion(+)
复制代码
修改 index.html

将 index.html 修改为以下内容
  1.           Our Feline Friends        <h1>Our Feline Friends</h1>    Eventually we will put cat pictures here.
  2.     <hr>  
复制代码
使用 git diff 命令查看更改的内容
  1. git diff
复制代码
输出
  1. On branch main
  2. Changes not staged for commit:
  3.   (use "git add <file>..." to update what will be committed)
  4.   (use "git restore <file>..." to discard changes in working directory)
  5.   modified:   index.html
  6. no changes added to commit (use "git add" and/or "git commit -a")
复制代码
默认使用 git diff 将工作树与索引进行比较。若要将工作树与上次提交进行比较,可以使用git diff HEAD。
提交更改。如果在索引中已具有文件,可以显示命令要暂存和提交的文件,而不是使用-a 标志。
  1. git commit -m "Add HTML boilerplate to index.html" index.html
复制代码
再次使用 git diff 将工作树与索引进行比较:
  1. git diff
复制代码
git diff 不会生成任何输出,因为工作树、索引和 HEAD 全部一致。
创建忽略提交的文件

新建名为.gitignore 的文件,将以下行添加到该文件中:
  1. *.bak
  2. *~
复制代码
此行指示 Git 忽略名称以.bak 或~结尾的文件。
使用以下命令来提交更改:
  1. git add -A
  2. git commit -m "Make small wording change; ignore editor backups"
复制代码
-A 选项与 git add 结合使用,以添加所有未跟踪(和未忽略)的文件,以及已更改并受 Git 控制的文件。
创建新的文件

在 CSS 目录下创建一个名为 site.css 的文件,并将以下内容添加到site.css 中。
  1. mkdir CSS
  2. cd CSS
  3. code site.css
复制代码
  1. h1, h2, h3, h4, h5, h6 { font-family: sans-serif; }
  2. body { font-family: serif; }
复制代码
修改 index.html,并将以下内容添加到 index.html 中,在行之后。
  1. [/code]使用 git status 查看已更改文件的摘要,并将未跟踪文件暂存到版本控制,并将所做更改提交。
  2. [code]git add .
  3. git commit -m "Add a simple stylesheet"
复制代码
列出提交

使用 git log 查看所有提交:
  1. git log
复制代码
输出
  1. commit 184bff431ac0c16b798d2dc5636d22fef68cbcaf (HEAD -> main)
  2. Author: duzhida <entercoder1993@gmail.com>
  3. Date:   Fri Feb 25 20:07:24 2022 +0800
  4.     Add HTML boilerplate to index.html
  5. commit 57cdf5c28b8b20fa75cc34aa0481308e2347f257
  6. Author: duzhida <entercoder1993@gmail.com>
  7. Date:   Fri Feb 25 20:00:05 2022 +0800
  8.     Add a heading to index.html
  9. commit 166b2d94a49ecbe6008aa027e9d2f7d870c78724
  10. Author: duzhida <entercoder1993@gmail.com>
  11. Date:   Fri Feb 25 19:54:08 2022 +0800
  12.     Create an empty index.html file
复制代码
添加—-oneline 参数可以使输出更加简洁:
  1. git log --oneline
复制代码
  1. 184bff4 (HEAD -> main) Add HTML boilerplate to index.html
  2. 57cdf5c Add a heading to index.html
  3. 166b2d9 Create an empty index.html file
复制代码
修改提交:--amend 标志

在上一个练习中,链接样式表的目录文件路径出现了错误,因此在 index.html 中更新了正确的路径。此时可以直接提交 index.html 的已更正版本,可以选择将其放在与原始版本相同的提交中。利用 git commit的 --amend 选项可以更改历史记录。
  1. git commit --amend --no-edit
复制代码
--no-edit 选项指示 Git 在不更改提交消息的情况下进行更改。 你还可以使用 --amend 来编辑提交消息、添加意外遗留在提交之外的文件或删除错误添加的文件。
更改历史记录是 Git 最强大的功能之一。 与使用大多数功能强大的工具一样,必须谨慎使用此功能。 特别注意,建议不要更改已与另一开发人员共享或已发布在共享存储库(如 GitHub)中的提交。
恢复已删除的文件

删除 index.html
  1. rm index.html
复制代码
恢复 index.html。使用 git checkout 恢复 index.html
  1. git checkout -- index.html
复制代码
输出
  1. Updated 1 path from the index
复制代码
恢复文件已删除的文件:git rm

使用 git rm 来删除文件,此命令会把文件从磁盘和 Git 索引中的记录文件删除。此时 git checkout -- index.html 将无法顺利恢复。
此时必须使用 git reset 取消暂存更改。
  1. git rm index.html
复制代码
此时使用 git checkout 无法恢复 index.html。因为这次 Git 不仅删除了该文件,还将删除操作记录在索引中。
输出
  1. error: pathspec 'index.html' did not match any file(s) known to git
复制代码
git reset 从 Git 取消文件删除的暂存。此命令会将文件返回到索引,但仍会在磁盘上删除该文件,此时就可以用 git checkout 将其从索引还原到磁盘。
  1. git reset HEAD index.html
复制代码
Git 提供多种重置类型。 默认类型为 --mixed,它会重置索引,但不会重置工作树;如果指定其他提交,它也会移动 HEAD。 --soft 选项仅移动 HEAD,且保持索引和工作树不变。 此选项会像 git status 那样,将所有更改保留为“待提交更改”。 --hard 重置会同时更改索引和工作树以匹配指定的提交;对跟踪文件所做的所有更改都会被丢弃。
输出
  1. Unstaged changes after reset:
  2. D  index.html
复制代码
现在可以使用 git checkout -- index.html 来恢复文件。
还原提交

假设修改了文件导致错误,要将文件恢复到先前的版本,但是更改已提交。这样就要还原先前的提交。
修改 index.html,用一下代码替换 index.html 中的内容并保存提交。
  1. <h1>That was a mistake!</h1>
复制代码
  1. git commit -m "Purposely overwrite the contents of index.html" index.html
  2. git log -n1
复制代码
此时使用 git revert 撤销已提交的更改
  1. git revert --no-edit HEAD
复制代码
--no-edit 表示不需要为此操作添加提交消息。
输出
  1. [main e2276d3] Revert "Purposely overwrite the contents of index.html"
  2. Date: Sun Feb 27 10:46:19 2022 +0800
  3. 1 file changed, 13 insertions(+), 1 deletion(-)
复制代码
使用 git log 显式最新的提交
  1. git log -n1
复制代码
输出
  1. commit e2276d3b8876d749315a11ac526f469afaee18c1 (HEAD -> main)
  2. Author: duzhida <entercoder1993@gmail.com>
  3. Date:   Sun Feb 27 10:46:19 2022 +0800
  4.     Revert "Purposely overwrite the contents of index.html"
  5.    
  6.     This reverts commit 229660d415c197288b499d1c7d2913534ae995f3.
复制代码
与 Git 协作

设置
  1. mkdir Cats
  2. cd Catsgit init --initial-branch=main
  3. git init -b maingit config user.name "cats"git config user.email "cats@gmail.com"touch index.htmlmkdir CSStouch CSS/site.cssgit add .git commit -m "Create empty index.html, site.css files"code index.htmlcode CSS/site.cssgit add .git commit -m "Add simple HTML and stylesheet"git log --oneline
复制代码
  1.           Our Feline Friends            <h1>Our Feline Friends</h1>    Eventually we will put cat pictures here.
  2.     <hr>  
复制代码
  1. h1, h2, h3, h4, h5, h6 { font-family: sans-serif; }
  2. body { font-family: serif; }
复制代码
输出
  1. 0d5e59f (HEAD -> main) Add simple HTML and stylesheet
  2. 07d4229 Create empty index.html, site.css files
复制代码
克隆存储库

模拟 Alice 将存储库克隆到他们的计算机上。在实际中,可以通过设置网络共享或可通过 URL 访问的远程存储库来完成此操作。
  1. cd ..
  2. mkdir Alice
  3. cd Alice
复制代码
使用 git clone 将项目目录中的存储库可能到 Alice 目录中。
  1. git clone ../Cats .
复制代码
输出
  1. Cloning into '.'...
  2. done.
复制代码
拉取请求

若存储仓库作出修改,可以使用 git pull 拉取最新的更改。
  1. git pull
复制代码
做出更改并提交拉取请求

Alice 更改了网站的背景色。
设置Alice本地存储库标识:
  1. git config user.name "alice"
  2. git config user.email "alice@gmail.com"
复制代码
  1. code CSS/site.css
复制代码
  1. body { font-family: serif; background-color: #F0FFF8; }
复制代码
提交更改
  1. git commit -a -m "Change background color to light blue"
复制代码
然后必须对原始存储库发出拉取请求
  1. git request-pull -p origin/main .
复制代码
输出
  1. warn: refs/heads/main found at . but points to a different object
  2. warn: Are you sure you pushed 'HEAD' there?
  3. The following changes since commit 0d5e59f17b0f6e6f8c7c6515abb55e398465fb59:
  4.   Add simple HTML and stylesheet (2022-02-27 10:58:42 +0800)
  5. are available in the Git repository at:
  6.   .
  7. for you to fetch changes up to 825aab8e6500f896f21b6c5aba8bdf4bec18dbe3:
  8.   Change background color to light blue (2022-02-27 11:30:27 +0800)
  9. ----------------------------------------------------------------
  10. alice (1):
  11.       Change background color to light blue
  12. CSS/site.css | 2 +-
  13. 1 file changed, 1 insertion(+), 1 deletion(-)
  14. diff --git a/CSS/site.css b/CSS/site.css
  15. index caefc86..86d41e8 100644
  16. --- a/CSS/site.css
  17. +++ b/CSS/site.css
  18. @@ -1,2 +1,2 @@
  19. h1, h2, h3, h4, h5, h6 { font-family: sans-serif; }
  20. -body { font-family: serif; }
  21. \ No newline at end of file
  22. +body { font-family: serif; background-color: #F0FFF8; }
  23. \ No newline at end of fil
复制代码
创建远程存储库并完成拉取操作

在实际中,Alice 目录位于 Alice 的计算机上。通过使用 git remote 命令设置远程存储库,然后将该远程库用于拉取和推送请求。
  1. cd ../Cats
  2. git remote add remote-alice ../Alice
复制代码
执行拉取,必须在拉取命令中指定分支 main
  1. git pull remote-alice main
复制代码
输出
  1. remote: Enumerating objects: 7, done.
  2. remote: Counting objects: 100% (7/7), done.
  3. remote: Compressing objects: 100% (3/3), done.
  4. remote: Total 4 (delta 1), reused 0 (delta 0), pack-reused 0
  5. Unpacking objects: 100% (4/4), 363 bytes | 363.00 KiB/s, done.
  6. From ../Alice
  7. * branch            main       -> FETCH_HEAD
  8. * [new branch]      main       -> remote-alice/main
  9. Updating 0d5e59f..825aab8
  10. Fast-forward
  11. CSS/site.css | 2 +-
  12. 1 file changed, 1 insertion(+), 1 deletion(-)
复制代码
创建共享存储库进行协作

你需要的是不包含工作树的存储库。 与工作树相比,空存储库具有多个优点:

  • 没有工作树,每个人都可以推送更改,而不必担心签出了哪个分支。
  • Git 可以轻松检测到其他用户是否推送了可能与你的更改冲突的更改。
  • 共享存储库可以扩展到任意数量的开发人员。 使用空存储库,你只需了解共享存储库,而不需要了解可能需要向其拉取的所有其他协作者。
  • 共享存储库放置在你们都可以完全访问的服务器上,无需担心防火墙和权限。
  • 你在服务器上不需要单独的帐户,因为 Git 会跟踪每个提交者。 (GitHub 有数百万用户共享 git 帐户。 每个人都使用安全外壳 (SSH) 加密网络协议,并且用户通过其公钥进行区分。)
在 Alice 和 Cats 目录的同一级创建一个名为 Shared.git 的新目录。
  1. mkdir Shared.git
  2. cd Shared.git
复制代码
创建空存储库:
  1. git init -bare
复制代码
当存储库仍为空时,git checkout 命令不能用于设置默认分支的名称。 若要完成此任务,可以将 HEAD 分支更改为指向不同的分支;在本例中,它是 main 分支:
  1. git symbolic-ref HEAD refs/heads/main
复制代码
将存储库内容放入共享存储库,设置 origin 远程存储库,并执行初始推送。
  1. cd ../Cats
  2. git remote add origin ../Shared.git
  3. git push origin main
复制代码
输出
  1. Enumerating objects: 13, done.
  2. Counting objects: 100% (13/13), done.
  3. Delta compression using up to 8 threads
  4. Compressing objects: 100% (9/9), done.
  5. Writing objects: 100% (13/13), 1.14 KiB | 1.14 MiB/s, done.
  6. Total 13 (delta 1), reused 0 (delta 0), pack-reused 0
  7. To ../Shared.git
  8. * [new branch]      main -> main
复制代码
如果希望 push 和 pull 在默认情况下使用 origin 和 main 分支,那你就需要告知 Git 需要跟踪的分支。
  1. git branch --set-upstream-to origin/main
复制代码
输出
  1. Branch 'main' set up to track remote branch 'main' from 'origin'.
复制代码
为协作者设置

让 Bob 克隆空存储库,然后让 Alice 在其存储库中设置元,以将共享存储卡库作为推送和拉取的目标。
  1. cd ..
  2. mkdir Bob
  3. cd Bob
复制代码
克隆共享存储库
  1. git clone ../Shared.git .
复制代码
目前 Alice 的存储库配置为从其自身的存储库进行推送和拉取,使用以下命令将 Alice 的 origin 指向共享存储库。
  1. cd ../Alice
  2. git remote set-url origin ../Shared.git
复制代码
开始协作

Bob 准备在网站的页面底部添加一个页脚。
  1. cd ../Bob
  2. git config user.name "bob"
  3. git config user.email "bob@gmail.com"
复制代码
打开 index.html,并将<hr>元素替换为此行。
  1. <footer><hr>Copyright (c) 2021 Contoso Cats</footer>
复制代码
提交更改并推送到远程源。
  1. git commit -a -m "Put a footer at the bottom of the page"
  2. git push
复制代码
输出
  1. Enumerating objects: 5, done.
  2. Counting objects: 100% (5/5), done.
  3. Delta compression using up to 8 threads
  4. Compressing objects: 100% (3/3), done.
  5. Writing objects: 100% (3/3), 379 bytes | 379.00 KiB/s, done.
  6. Total 3 (delta 1), reused 0 (delta 0), pack-reused 0
  7. To /Users/entercoder/Desktop/bob/../Shared.git
  8.    825aab8..6594c56  main -> main
复制代码
Alice 决定在页面上添加导航栏。
  1. cd ../Alice
  2. code index.html
  3. code CSS/site.css
复制代码
  1. <nav>home</nav>
复制代码
  1. nav { background-color: #C0D8DF; }
复制代码
Alice 在进行提交前应该拉取 bob 的更改。
  1. git pull
复制代码
输出
  1. remote: Enumerating objects: 5, done.
  2. remote: Counting objects: 100% (5/5), done.
  3. remote: Compressing objects: 100% (3/3), done.
  4. remote: Total 3 (delta 1), reused 0 (delta 0), pack-reused 0
  5. Unpacking objects: 100% (3/3), 359 bytes | 359.00 KiB/s, done.
  6. From ../Shared
  7.    825aab8..6594c56  main       -> origin/main
  8. Updating 825aab8..6594c56
  9. error: Your local changes to the following files would be overwritten by merge:
  10.   index.html
  11. Please commit your changes or stash them before you merge.
  12. Aborting
复制代码
使用 git diff 查看 bob 对 index.html 所做的修改。
  1. git diff origin -- index.html
复制代码
在尝试拉取之前应该储藏或提交更改。拉取到一个脏的工作树是有风险的,它会执行一些你不容易恢复的操作。
git stash 通过执行一些临时提交来保存工作树和索引的状态。 将储藏视为在执行其他操作时保存当前工作的一种方法,而不会做出“真正的”提交或影响存储库历史记录。
  1. git stash push
  2. # 弹出储藏
  3. git stash pop
复制代码
提交更改到共享存储库中。
  1. git push
复制代码
返回项目目录执行拉取操作。
  1. cd ../Cats
  2. git pull
复制代码
bob 的存储库也要同步更新保持最新的状态。
  1. git pull
复制代码
创建分支与合并编辑代码

设置共享存储库
  1. mkdir Shared.git
  2. cd Shared.gitgit init --baregit symbolic-ref HEAD refs/heads/main
复制代码
克隆共享存储库

为 Bob 克隆共享存储库
  1. cd ..
  2. mkdir bob
  3. git clone ../Shared.git .
  4. git config user.name bob
  5. git config user.email bob@gmail.com
  6. git symbolic-ref HEAD refs/heads/main
复制代码
添加基本文件
  1. touch index.html
  2. mkdir Assets
  3. touch Assets/site.css
  4. git add .
  5. git commit -m "Create empty index.html and site.css file"
  6. code index.html
  7. code Assets/site.css
  8. git add .
  9. git commit -m "Add simple HTML and stylesheet"
  10. git push --set-upstream origin main
复制代码
  1.           Our Feline Friends            <nav>home</nav>    <h1>Our Feline Friends</h1>    Eventually we will put cat pictures here.
  2.     <footer><hr>Copyright (c) 2021 Contoso Cats</footer>  
复制代码
  1. h1, h2, h3, h4, h5, h6 { font-family: sans-serif; }
  2. body { font-family: serif; background-color: #F0FFF8; }
  3. nav, footer { background-color: #C0D8DF; }
复制代码
输出
  1. Enumerating objects: 9, done.
  2. Counting objects: 100% (9/9), done.
  3. Delta compression using up to 8 threads
  4. Compressing objects: 100% (6/6), done.
  5. Writing objects: 100% (9/9), 952 bytes | 952.00 KiB/s, done.
  6. Total 9 (delta 0), reused 0 (delta 0), pack-reused 0
  7. To /Users/entercoder/bob/../Shared.git
  8. * [new branch]      main -> main
  9. Branch 'main' set up to track remote branch 'main' from 'origin'.
复制代码
为 Alice 创建分支

Alice 创建了一个 add-style 的主题分支以完成工作。
  1. cd ..
  2. mkdir Alice
  3. cd Alicegit clone ../Shared.git .git config user.name alicegit config user.email alice@gmail.comgit branch add-stylegit checkout add-style
复制代码
打开 site.css,添加以下代码。
  1. .cat { max-width: 40%; padding: 5 }
复制代码
保存文件并提交。
  1. git commit -a -m "Add style for cat pictures"
复制代码
Alice将更改推送到共享存储库。
  1. git checkout main
  2. git pull
复制代码
输出显式 main 分支是最新的。因此 Alice 通过运行 git merge --ff-only 执行快速合并来向 add-style 分支合并到 main 分支。然后将 main 从其存储库推送到共享存储库。
  1. git merge --ff-only add-style
  2. git push
复制代码
输出
  1. Updating ba17c91..592b108
  2. Fast-forward
  3. Assets/site.css | 3 ++-
  4. Enumerating objects: 7, done.
  5. Counting objects: 100% (7/7), done.
  6. Delta compression using up to 8 threads
  7. Compressing objects: 100% (3/3), done.
  8. Writing objects: 100% (4/4), 391 bytes | 391.00 KiB/s, done.
  9. Total 4 (delta 1), reused 0 (delta 0), pack-reused 0
  10. To /Users/entercoder/alice/../Shared.git
  11.    ba17c91..592b108  main -> main
复制代码
为 Bob 创建分支

返回 Bob 目录,创建 add-cat 分支。
  1. cd ../bob
  2. git checkout -b add-cat
复制代码
下载网站资源并将 bobcat2 图片移动到 Assets 目录,并删除其他文件。
  1. wget https://github.com/MicrosoftDocs/mslearn-branch-merge-git/raw/main/git-resources.zip
  2. unzip git-resources.zip
  3. mv bobcat2-317x240.jpg https://www.cnblogs.com/Assets/bobcat2-317x240.jpg
  4. rm git-resources.zip
  5. rm bombay-cat-180x240.jpg
复制代码
修改 index.html并将显示“Eventually we will put cat pictures here”的行替换为以下行:
  1. <img src="https://www.cnblogs.com/Assets/bobcat2-317x240.jpg" />
复制代码
保存文件并更改。
  1. git add .
  2. git commit -a -m "Add picture of Bob's cat"
复制代码
现在执行与 Alice 前面执行的操作相同。
  1. git checkout main
  2. git pull
复制代码
输出表示共享存储库中的 main 分支已进行过更改。并且已经与 bob 的存储库的 main 分支合并。
  1. remote: Enumerating objects: 7, done.
  2. remote: Counting objects: 100% (7/7), done.
  3. remote: Compressing objects: 100% (3/3), done.
  4. remote: Total 4 (delta 1), reused 0 (delta 0), pack-reused 0
  5. Unpacking objects: 100% (4/4), 371 bytes | 185.00 KiB/s, done.
  6. From /Users/entercoder/bob/../Shared
  7.    ba17c91..592b108  main       -> origin/main
  8. Updating ba17c91..592b108
  9. Fast-forward
  10. Assets/site.css | 3 ++-
  11. 1 file changed, 2 insertions(+), 1 deletion(-)
复制代码
接下来将其他分支合并到 main 分支,然后将自己存储库的 main 分支推送到共享存储库的 main 分支。
  1. git merge add-cat --no-edit
  2. git push
复制代码
同步存储库

切换到 Alice 目录下,使用 git pull 从共享存储库拉取最新版本。
  1. cd ../alice
  2. git pull
复制代码
解决合并冲突

为 alice 和 bob 分别创建一个分支,并同时进行修改。
  1. git checkout -b add-cat
  2. cd ../bob
  3. git checkout -b style-cat
复制代码
Alice 进行修改
  1. cd ../alice
  2. wget https://github.com/MicrosoftDocs/mslearn-branch-merge-git/raw/main/git-resources.zip
  3. unzip git-resources.zip
  4. mv bombay-cat-180x240.jpg https://www.cnblogs.com/Assets/bombay-cat-180x240.jpg
  5. rm git-resources.zip
  6. rm bobcat2-317x240.jpg
复制代码
打开 index.html,将 bob 的图片替换为 alice 的图片。
  1. code index.html
复制代码
  1. <img  src="https://www.cnblogs.com/Assets/bombay-cat-180x240.jpg" />
复制代码
alice 提交更改并将 add-cat 分支合并到 main 分支,并推送到存储库中。
  1. git add Assetsgit commit -a -m "Add picture of Alice's cat"git checkout main
  2. git pullgit merge --ff-only-add-catgit push
复制代码
Bob 进行修改

打开 bob 的目录,将 属性添加到<img>元素
  1. <img  src="https://www.cnblogs.com/Assets/bobcat2-317x240.jpg" />
复制代码
保存提交,切换到 main 分支,运行 git pull,然后合并样式更改。
  1. git commit -a -m "Style Bob's cat"git checkout main
  2. git pullgit merge style-cat
复制代码
输出显示合并冲突了,两个人更改了同一行。
  1. Auto-merging index.html
  2. CONFLICT (content): Merge conflict in index.html
  3. Removing https://www.cnblogs.com/Assets/bobcat2-317x240.jpg
  4. Automatic merge failed; fix conflicts and then commit the result.
复制代码
解决合并冲突

此时 Bob 有几种选择。 Bob 可以执行下列操作之一:

  • 使用 git merge --abort 命令将 main分支还原到它尝试合并之前分支所处于的状态。 运行 git pull 命令获取 Alice 做出的更改。 然后,创建一个新分支,完成更改,并将其分支合并到main分支中。 最后,推送其更改。
  • 运行 git reset --hard 命令返回到他们启动合并之前的状态。
  • 使用 Git 在受影响的文件中插入的信息手动解决冲突。
  1.           Our Feline Friends            <nav>home</nav>    <h1>Our Feline Friends</h1>> 139f75ab70619612b1f597f72d949d0ec1955d79    <footer><hr>Copyright (c) 2021 Contoso Cats</footer>  
复制代码
Git 使用特殊格式来帮助你识别和解决冲突:左尖括号 。 短划线 ======= 上方的内容显示你的分支中的更改。 分隔符下面的内容显示你尝试合并到的分支中相应内容的版本。
我们通过编辑“index.html”文件来解决此合并冲突。 由于此合并冲突可快速修复,因此你可直接在main分支中进行更改。
删除特殊的格式设置行,保留两个图片。
  1. <<<<<<< HEAD
  2. =======
  3. >>>>>>> style-cat
复制代码
运行一下命令提交更改。并推送到远程 main 分支。
  1. git add index.html
  2. git commit -a -m "Style Bob's cat"
  3. git push
复制代码
将更改同步到 alice 的存储库。
  1. cd ../alice
  2. git pull
复制代码
来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
1.jpg
3.jpg
4.jpg

相关推荐

3 天前

举报

热心回复!
您需要登录后才可以回帖 登录 | 立即注册