Git基础操作及协作流程

您所在的位置:网站首页 git替换本地代码 Git基础操作及协作流程

Git基础操作及协作流程

2023-02-27 13:15| 来源: 网络整理| 查看: 265

Git基础操作及协作流程 一整套流程帮你实践整个 Git 操作基础流程。

一整套流程帮你实践整个 Git 操作基础及协作流程。

来源:https://docs.microsoft.com/zh-cn/learn/paths/intro-to-vc-git/

Git 介绍 配置 Git

确认已经安装 git

git --version

输出

git version 2.30.1 (Apple Git-130)

配置 Git,必须定义一些全局变量:user.name 和 user.email。

git config --global user.name "" git config --global user.email ""

检查更改是否成功。

git config --list

输出

user.name=User Name [email protected] 配置 Git 存储库

创建名为“Cats”的文件夹,此文件夹为项目目录(也称为“工作树”)

mkdir Cats cd Cats

初始化新存储库,将默认分支名称设置为 main:

git init --initial-branch=main git init -b main

使用 git status 显示工作树的状态:

git status

输出

On branch main No commits yet nothing to commit (create/copy files and use "git add" to track)

使用 ls -a 显示工作树的内容:

ls -a

确认目录包含一个名为.git 的子目录。此文件夹为 Git 存储库——用于存储工作树的元数据和历史记录的目录。

从 Git 获取帮助 git --help

输出

usage: git [--version] [--help] [-C ] [-c =] [--exec-path[=]] [--html-path] [--man-path] [--info-path] [-p | --paginate | -P | --no-pager] [--no-replace-objects] [--bare] [--git-dir=] [--work-tree=] [--namespace=] [] These are common Git commands used in various situations: start a working area (see also: git help tutorial) clone Clone a repository into a new directory init Create an empty Git repository or reinitialize an existing one work on the current change (see also: git help everyday) add Add file contents to the index mv Move or rename a file, a directory, or a symlink restore Restore working tree files rm Remove files from the working tree and from the index sparse-checkout Initialize and modify the sparse-checkout examine the history and state (see also: git help revisions) bisect Use binary search to find the commit that introduced a bug diff Show changes between commits, commit and working tree, etc grep Print lines matching a pattern log Show commit logs show Show various types of objects status Show the working tree status grow, mark and tweak your common history branch List, create, or delete branches commit Record changes to the repository merge Join two or more development histories together rebase Reapply commits on top of another base tip reset Reset current HEAD to the specified state switch Switch branches tag Create, list, delete or verify a tag object signed with GPG collaborate (see also: git help workflows) fetch Download objects and refs from another repository pull Fetch from and integrate with another repository or a local branch push Update remote refs along with associated objects 'git help -a' and 'git help -g' list available subcommands and some concept guides. See 'git help ' or 'git help ' to read about a specific subcommand or concept. See 'git help git' for an overview of the system. 创建和修改 Git 项目 创建和添加(暂存)文件

使用 touch创建名为 index.html 的文件

touch index.html

如果文件存在,touch 会更新文件的上次修改时间。

使用 git status 获取工作树的状态:

git status

输出:

On branch main No commits yet Untracked files: (use "git add ..." to include in what will be committed) index.html nothing added to commit but untracked files present (use "git add" to track)

使用 git add 将新文件添加到 Git 的“index”,然后是用 git status 检查状态。. 表示为当前目录中的所有文件添加编制索引。

git add .

使用 git status 以确保更改已暂存无误:

git status

输出

On branch main No commits yet Changes to be committed: (use "git rm --cached ..." to unstage) new file: index.html 进行首次提交

使用以下命令创建新提交

git commit index.html -m "Create an empty index.html file"

输出

[main (root-commit) 166b2d9] Create an empty index.html file 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 index.html

使用 git log 显式有关提交的信息:

git log

输出

commit 166b2d94a49ecbe6008aa027e9d2f7d870c78724 (HEAD -> main) Author: duzhida Date: Fri Feb 25 19:54:08 2022 +0800 Create an empty index.html file 修改 index.html 并提交更改

修改 index.html,将以下语句粘贴到编辑器。

Our Feline Friends

使用 git status 检查工作树状态:

git status

输出

On branch main Changes not staged for commit: (use "git add ..." to update what will be committed) (use "git restore ..." to discard changes in working directory) modified: index.html no changes added to commit (use "git add" and/or "git commit -a")

提交更改

git commit -a -m "Add a heading to index.html"

输出

[main 57cdf5c] Add a heading to index.html 1 file changed, 1 insertion(+) 修改 index.html

将 index.html 修改为以下内容

Our Feline Friends Our Feline Friends

Eventually we will put cat pictures here.

使用 git diff 命令查看更改的内容

git diff

输出

On branch main Changes not staged for commit: (use "git add ..." to update what will be committed) (use "git restore ..." to discard changes in working directory) modified: index.html no changes added to commit (use "git add" and/or "git commit -a")

默认使用 git diff 将工作树与索引进行比较。若要将工作树与上次提交进行比较,可以使用git diff HEAD。

提交更改。如果在索引中已具有文件,可以显示命令要暂存和提交的文件,而不是使用-a 标志。

git commit -m "Add HTML boilerplate to index.html" index.html

再次使用 git diff 将工作树与索引进行比较:

git diff

git diff 不会生成任何输出,因为工作树、索引和 HEAD 全部一致。

创建忽略提交的文件

新建名为.gitignore 的文件,将以下行添加到该文件中:

*.bak *~

此行指示 Git 忽略名称以.bak 或~结尾的文件。

使用以下命令来提交更改:

git add -A git commit -m "Make small wording change; ignore editor backups"

-A 选项与 git add 结合使用,以添加所有未跟踪(和未忽略)的文件,以及已更改并受 Git 控制的文件。

创建新的文件

在 CSS 目录下创建一个名为 site.css 的文件,并将以下内容添加到site.css 中。

mkdir CSS cd CSS code site.css h1, h2, h3, h4, h5, h6 { font-family: sans-serif; } body { font-family: serif; }

修改 index.html,并将以下内容添加到 index.html 中,在行之后。

使用 git status 查看已更改文件的摘要,并将未跟踪文件暂存到版本控制,并将所做更改提交。

git add . git commit -m "Add a simple stylesheet" 列出提交

使用 git log 查看所有提交:

git log

输出

commit 184bff431ac0c16b798d2dc5636d22fef68cbcaf (HEAD -> main) Author: duzhida Date: Fri Feb 25 20:07:24 2022 +0800 Add HTML boilerplate to index.html commit 57cdf5c28b8b20fa75cc34aa0481308e2347f257 Author: duzhida Date: Fri Feb 25 20:00:05 2022 +0800 Add a heading to index.html commit 166b2d94a49ecbe6008aa027e9d2f7d870c78724 Author: duzhida Date: Fri Feb 25 19:54:08 2022 +0800 Create an empty index.html file

添加—-oneline 参数可以使输出更加简洁:

git log --oneline 184bff4 (HEAD -> main) Add HTML boilerplate to index.html 57cdf5c Add a heading to index.html 166b2d9 Create an empty index.html file 修改提交:--amend 标志

在上一个练习中,链接样式表的目录文件路径出现了错误,因此在 index.html 中更新了正确的路径。此时可以直接提交 index.html 的已更正版本,可以选择将其放在与原始版本相同的提交中。利用 git commit的 --amend 选项可以更改历史记录。

git commit --amend --no-edit

--no-edit 选项指示 Git 在不更改提交消息的情况下进行更改。 你还可以使用 --amend 来编辑提交消息、添加意外遗留在提交之外的文件或删除错误添加的文件。

更改历史记录是 Git 最强大的功能之一。 与使用大多数功能强大的工具一样,必须谨慎使用此功能。 特别注意,建议不要更改已与另一开发人员共享或已发布在共享存储库(如 GitHub)中的提交。

恢复已删除的文件

删除 index.html

rm index.html

恢复 index.html。使用 git checkout 恢复 index.html

git checkout -- index.html

输出

Updated 1 path from the index 恢复文件已删除的文件:git rm

使用 git rm 来删除文件,此命令会把文件从磁盘和 Git 索引中的记录文件删除。此时 git checkout -- index.html 将无法顺利恢复。

此时必须使用 git reset 取消暂存更改。

git rm index.html

此时使用 git checkout 无法恢复 index.html。因为这次 Git 不仅删除了该文件,还将删除操作记录在索引中。

输出

error: pathspec 'index.html' did not match any file(s) known to git

git reset 从 Git 取消文件删除的暂存。此命令会将文件返回到索引,但仍会在磁盘上删除该文件,此时就可以用 git checkout 将其从索引还原到磁盘。

git reset HEAD index.html

Git 提供多种重置类型。 默认类型为 --mixed,它会重置索引,但不会重置工作树;如果指定其他提交,它也会移动 HEAD。 --soft 选项仅移动 HEAD,且保持索引和工作树不变。 此选项会像 git status 那样,将所有更改保留为“待提交更改”。 --hard 重置会同时更改索引和工作树以匹配指定的提交;对跟踪文件所做的所有更改都会被丢弃。

输出

Unstaged changes after reset: D index.html

现在可以使用 git checkout -- index.html 来恢复文件。

还原提交

假设修改了文件导致错误,要将文件恢复到先前的版本,但是更改已提交。这样就要还原先前的提交。

修改 index.html,用一下代码替换 index.html 中的内容并保存提交。

That was a mistake! git commit -m "Purposely overwrite the contents of index.html" index.html git log -n1

此时使用 git revert 撤销已提交的更改

git revert --no-edit HEAD

--no-edit 表示不需要为此操作添加提交消息。

输出

[main e2276d3] Revert "Purposely overwrite the contents of index.html" Date: Sun Feb 27 10:46:19 2022 +0800 1 file changed, 13 insertions(+), 1 deletion(-)

使用 git log 显式最新的提交

git log -n1

输出

commit e2276d3b8876d749315a11ac526f469afaee18c1 (HEAD -> main) Author: duzhida Date: Sun Feb 27 10:46:19 2022 +0800 Revert "Purposely overwrite the contents of index.html" This reverts commit 229660d415c197288b499d1c7d2913534ae995f3. 与 Git 协作 设置 mkdir Cats cd Cats git init --initial-branch=main git init -b main git config user.name "cats" git config user.email "[email protected]" touch index.html mkdir CSS touch CSS/site.css git add . git commit -m "Create empty index.html, site.css files" code index.html code CSS/site.css git add . git commit -m "Add simple HTML and stylesheet" git log --oneline Our Feline Friends Our Feline Friends

Eventually we will put cat pictures here.

h1, h2, h3, h4, h5, h6 { font-family: sans-serif; } body { font-family: serif; }

输出

0d5e59f (HEAD -> main) Add simple HTML and stylesheet 07d4229 Create empty index.html, site.css files 克隆存储库

模拟 Alice 将存储库克隆到他们的计算机上。在实际中,可以通过设置网络共享或可通过 URL 访问的远程存储库来完成此操作。

cd .. mkdir Alice cd Alice

使用 git clone 将项目目录中的存储库可能到 Alice 目录中。

git clone ../Cats .

输出

Cloning into '.'... done. 拉取请求

若存储仓库作出修改,可以使用 git pull 拉取最新的更改。

git pull 做出更改并提交拉取请求

Alice 更改了网站的背景色。

设置Alice本地存储库标识:

git config user.name "alice" git config user.email "[email protected]" code CSS/site.css body { font-family: serif; background-color: #F0FFF8; }

提交更改

git commit -a -m "Change background color to light blue"

然后必须对原始存储库发出拉取请求

git request-pull -p origin/main .

输出

warn: refs/heads/main found at . but points to a different object warn: Are you sure you pushed 'HEAD' there? The following changes since commit 0d5e59f17b0f6e6f8c7c6515abb55e398465fb59: Add simple HTML and stylesheet (2022-02-27 10:58:42 +0800) are available in the Git repository at: . for you to fetch changes up to 825aab8e6500f896f21b6c5aba8bdf4bec18dbe3: Change background color to light blue (2022-02-27 11:30:27 +0800) ---------------------------------------------------------------- alice (1): Change background color to light blue CSS/site.css | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CSS/site.css b/CSS/site.css index caefc86..86d41e8 100644 --- a/CSS/site.css +++ b/CSS/site.css @@ -1,2 +1,2 @@ h1, h2, h3, h4, h5, h6 { font-family: sans-serif; } -body { font-family: serif; } \ No newline at end of file +body { font-family: serif; background-color: #F0FFF8; } \ No newline at end of fil 创建远程存储库并完成拉取操作

在实际中,Alice 目录位于 Alice 的计算机上。通过使用 git remote 命令设置远程存储库,然后将该远程库用于拉取和推送请求。

cd ../Cats git remote add remote-alice ../Alice

执行拉取,必须在拉取命令中指定分支 main

git pull remote-alice main

输出

remote: Enumerating objects: 7, done. remote: Counting objects: 100% (7/7), done. remote: Compressing objects: 100% (3/3), done. remote: Total 4 (delta 1), reused 0 (delta 0), pack-reused 0 Unpacking objects: 100% (4/4), 363 bytes | 363.00 KiB/s, done. From ../Alice * branch main -> FETCH_HEAD * [new branch] main -> remote-alice/main Updating 0d5e59f..825aab8 Fast-forward CSS/site.css | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 创建共享存储库进行协作

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

没有工作树,每个人都可以推送更改,而不必担心签出了哪个分支。 Git 可以轻松检测到其他用户是否推送了可能与你的更改冲突的更改。 共享存储库可以扩展到任意数量的开发人员。 使用空存储库,你只需了解共享存储库,而不需要了解可能需要向其拉取的所有其他协作者。 共享存储库放置在你们都可以完全访问的服务器上,无需担心防火墙和权限。 你在服务器上不需要单独的帐户,因为 Git 会跟踪每个提交者。 (GitHub 有数百万用户共享 git 帐户。 每个人都使用安全外壳 (SSH) 加密网络协议,并且用户通过其公钥进行区分。)

在 Alice 和 Cats 目录的同一级创建一个名为 Shared.git 的新目录。

mkdir Shared.git cd Shared.git

创建空存储库:

git init -bare

当存储库仍为空时,git checkout 命令不能用于设置默认分支的名称。 若要完成此任务,可以将 HEAD 分支更改为指向不同的分支;在本例中,它是 main 分支:

git symbolic-ref HEAD refs/heads/main

将存储库内容放入共享存储库,设置 origin 远程存储库,并执行初始推送。

cd ../Cats git remote add origin ../Shared.git git push origin main

输出

Enumerating objects: 13, done. Counting objects: 100% (13/13), done. Delta compression using up to 8 threads Compressing objects: 100% (9/9), done. Writing objects: 100% (13/13), 1.14 KiB | 1.14 MiB/s, done. Total 13 (delta 1), reused 0 (delta 0), pack-reused 0 To ../Shared.git * [new branch] main -> main

如果希望 push 和 pull 在默认情况下使用 origin 和 main 分支,那你就需要告知 Git 需要跟踪的分支。

git branch --set-upstream-to origin/main

输出

Branch 'main' set up to track remote branch 'main' from 'origin'. 为协作者设置

让 Bob 克隆空存储库,然后让 Alice 在其存储库中设置元,以将共享存储卡库作为推送和拉取的目标。

cd .. mkdir Bob cd Bob

克隆共享存储库

git clone ../Shared.git .

目前 Alice 的存储库配置为从其自身的存储库进行推送和拉取,使用以下命令将 Alice 的 origin 指向共享存储库。

cd ../Alice git remote set-url origin ../Shared.git 开始协作

Bob 准备在网站的页面底部添加一个页脚。

cd ../Bob git config user.name "bob" git config user.email "[email protected]"

打开 index.html,并将元素替换为此行。

Copyright (c) 2021 Contoso Cats

提交更改并推送到远程源。

git commit -a -m "Put a footer at the bottom of the page" git push

输出

Enumerating objects: 5, done. Counting objects: 100% (5/5), done. Delta compression using up to 8 threads Compressing objects: 100% (3/3), done. Writing objects: 100% (3/3), 379 bytes | 379.00 KiB/s, done. Total 3 (delta 1), reused 0 (delta 0), pack-reused 0 To /Users/entercoder/Desktop/bob/../Shared.git 825aab8..6594c56 main -> main

Alice 决定在页面上添加导航栏。

cd ../Alice code index.html code CSS/site.css home nav { background-color: #C0D8DF; }

Alice 在进行提交前应该拉取 bob 的更改。

git pull

输出

remote: Enumerating objects: 5, done. remote: Counting objects: 100% (5/5), done. remote: Compressing objects: 100% (3/3), done. remote: Total 3 (delta 1), reused 0 (delta 0), pack-reused 0 Unpacking objects: 100% (3/3), 359 bytes | 359.00 KiB/s, done. From ../Shared 825aab8..6594c56 main -> origin/main Updating 825aab8..6594c56 error: Your local changes to the following files would be overwritten by merge: index.html Please commit your changes or stash them before you merge. Aborting

使用 git diff 查看 bob 对 index.html 所做的修改。

git diff origin -- index.html

在尝试拉取之前应该储藏或提交更改。拉取到一个脏的工作树是有风险的,它会执行一些你不容易恢复的操作。

git stash 通过执行一些临时提交来保存工作树和索引的状态。 将储藏视为在执行其他操作时保存当前工作的一种方法,而不会做出“真正的”提交或影响存储库历史记录。

git stash push # 弹出储藏 git stash pop

提交更改到共享存储库中。

git push

返回项目目录执行拉取操作。

cd ../Cats git pull

bob 的存储库也要同步更新保持最新的状态。

git pull 创建分支与合并编辑代码 设置共享存储库 mkdir Shared.git cd Shared.git git init --bare git symbolic-ref HEAD refs/heads/main 克隆共享存储库

为 Bob 克隆共享存储库

cd .. mkdir bob git clone ../Shared.git . git config user.name bob git config user.email [email protected] git symbolic-ref HEAD refs/heads/main

添加基本文件

touch index.html mkdir Assets touch Assets/site.css git add . git commit -m "Create empty index.html and site.css file" code index.html code Assets/site.css git add . git commit -m "Add simple HTML and stylesheet" git push --set-upstream origin main Our Feline Friends home Our Feline Friends

Eventually we will put cat pictures here.

Copyright (c) 2021 Contoso Cats h1, h2, h3, h4, h5, h6 { font-family: sans-serif; } body { font-family: serif; background-color: #F0FFF8; } nav, footer { background-color: #C0D8DF; }

输出

Enumerating objects: 9, done. Counting objects: 100% (9/9), done. Delta compression using up to 8 threads Compressing objects: 100% (6/6), done. Writing objects: 100% (9/9), 952 bytes | 952.00 KiB/s, done. Total 9 (delta 0), reused 0 (delta 0), pack-reused 0 To /Users/entercoder/bob/../Shared.git * [new branch] main -> main Branch 'main' set up to track remote branch 'main' from 'origin'. 为 Alice 创建分支

Alice 创建了一个 add-style 的主题分支以完成工作。

cd .. mkdir Alice cd Alice git clone ../Shared.git . git config user.name alice git config user.email [email protected] git branch add-style git checkout add-style

打开 site.css,添加以下代码。

.cat { max-width: 40%; padding: 5 }

保存文件并提交。

git commit -a -m "Add style for cat pictures"

Alice将更改推送到共享存储库。

git checkout main git pull

输出显式 main 分支是最新的。因此 Alice 通过运行 git merge --ff-only 执行快速合并来向 add-style 分支合并到 main 分支。然后将 main 从其存储库推送到共享存储库。

git merge --ff-only add-style git push

输出

Updating ba17c91..592b108 Fast-forward Assets/site.css | 3 ++- Enumerating objects: 7, done. Counting objects: 100% (7/7), done. Delta compression using up to 8 threads Compressing objects: 100% (3/3), done. Writing objects: 100% (4/4), 391 bytes | 391.00 KiB/s, done. Total 4 (delta 1), reused 0 (delta 0), pack-reused 0 To /Users/entercoder/alice/../Shared.git ba17c91..592b108 main -> main 为 Bob 创建分支

返回 Bob 目录,创建 add-cat 分支。

cd ../bob git checkout -b add-cat

下载网站资源并将 bobcat2 图片移动到 Assets 目录,并删除其他文件。

wget https://github.com/MicrosoftDocs/mslearn-branch-merge-git/raw/main/git-resources.zip unzip git-resources.zip mv bobcat2-317x240.jpg Assets/bobcat2-317x240.jpg rm git-resources.zip rm bombay-cat-180x240.jpg

修改 index.html并将显示“Eventually we will put cat pictures here”的行替换为以下行:

保存文件并更改。

git add . git commit -a -m "Add picture of Bob's cat"

现在执行与 Alice 前面执行的操作相同。

git checkout main git pull

输出表示共享存储库中的 main 分支已进行过更改。并且已经与 bob 的存储库的 main 分支合并。

remote: Enumerating objects: 7, done. remote: Counting objects: 100% (7/7), done. remote: Compressing objects: 100% (3/3), done. remote: Total 4 (delta 1), reused 0 (delta 0), pack-reused 0 Unpacking objects: 100% (4/4), 371 bytes | 185.00 KiB/s, done. From /Users/entercoder/bob/../Shared ba17c91..592b108 main -> origin/main Updating ba17c91..592b108 Fast-forward Assets/site.css | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)

接下来将其他分支合并到 main 分支,然后将自己存储库的 main 分支推送到共享存储库的 main 分支。

git merge add-cat --no-edit git push 同步存储库

切换到 Alice 目录下,使用 git pull 从共享存储库拉取最新版本。

cd ../alice git pull 解决合并冲突

为 alice 和 bob 分别创建一个分支,并同时进行修改。

git checkout -b add-cat cd ../bob git checkout -b style-cat Alice 进行修改 cd ../alice wget https://github.com/MicrosoftDocs/mslearn-branch-merge-git/raw/main/git-resources.zip unzip git-resources.zip mv bombay-cat-180x240.jpg Assets/bombay-cat-180x240.jpg rm git-resources.zip rm bobcat2-317x240.jpg

打开 index.html,将 bob 的图片替换为 alice 的图片。

code index.html

alice 提交更改并将 add-cat 分支合并到 main 分支,并推送到存储库中。

git add Assets git commit -a -m "Add picture of Alice's cat" git checkout main git pull git merge --ff-only-add-cat git push Bob 进行修改

打开 bob 的目录,将 class="cat"属性添加到元素

保存提交,切换到 main 分支,运行 git pull,然后合并样式更改。

git commit -a -m "Style Bob's cat" git checkout main git pull git merge style-cat

输出显示合并冲突了,两个人更改了同一行。

Auto-merging index.html CONFLICT (content): Merge conflict in index.html Removing Assets/bobcat2-317x240.jpg Automatic merge failed; fix conflicts and then commit the result. 解决合并冲突

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

使用 git merge --abort 命令将 main分支还原到它尝试合并之前分支所处于的状态。 运行 git pull 命令获取 Alice 做出的更改。 然后,创建一个新分支,完成更改,并将其分支合并到main分支中。 最后,推送其更改。 运行 git reset --hard 命令返回到他们启动合并之前的状态。 使用 Git 在受影响的文件中插入的信息手动解决冲突。 Our Feline Friends home Our Feline Friends > 139f75ab70619612b1f597f72d949d0ec1955d79 Copyright (c) 2021 Contoso Cats

Git 使用特殊格式来帮助你识别和解决冲突:左尖括号 。 短划线 ======= 上方的内容显示你的分支中的更改。 分隔符下面的内容显示你尝试合并到的分支中相应内容的版本。

我们通过编辑“index.html”文件来解决此合并冲突。 由于此合并冲突可快速修复,因此你可直接在main分支中进行更改。 删除特殊的格式设置行,保留两个图片。

> style-cat

运行一下命令提交更改。并推送到远程 main 分支。

git add index.html git commit -a -m "Style Bob's cat" git push

将更改同步到 alice 的存储库。

cd ../alice git pull

本文来自博客园,作者:Solita1y,转载请注明原文链接:https://www.cnblogs.com/solita1y/p/17154622.html



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3