介绍
在这个实验(Lab)中,你将学习如何处理一个常见的 Git 场景:当你的本地分支领先于远程仓库时。这种情况发生在你在本地进行了提交,但尚未将它们推送到远程仓库。实验结束时,你将了解如何识别何时你的分支领先于远程分支,以及如何正确地同步你的本地和远程仓库。
在这个实验(Lab)中,你将学习如何处理一个常见的 Git 场景:当你的本地分支领先于远程仓库时。这种情况发生在你在本地进行了提交,但尚未将它们推送到远程仓库。实验结束时,你将了解如何识别何时你的分支领先于远程分支,以及如何正确地同步你的本地和远程仓库。
在这一步中,你将对本地仓库进行更改,这些更改尚未推送到远程仓库,从而创建“领先于 origin”的场景。
首先,我们需要设置一个可以操作的仓库。我们将使用 git-playground 仓库作为起点。由于你需要推送权限来完成这个实验(Lab),你需要先 Fork 该仓库。
现在,我们将你的 Forked 仓库克隆到本地机器。将 YOUR_USERNAME 替换为你的实际 GitHub 用户名:
cd ~/project
git clone https://github.com/YOUR_USERNAME/git-playground.git
cd git-playground
克隆后,验证远程仓库的配置是否正确:
git remote -v
你应该会看到输出显示你的 Forked 仓库作为 origin 远程仓库:
origin https://github.com/YOUR_USERNAME/git-playground.git (fetch)
origin https://github.com/YOUR_USERNAME/git-playground.git (push)
Git 采用分布式模型运作,每个开发者在本地机器上都拥有仓库的完整副本。在本地所做的更改需要显式地与远程仓库同步。
现在,让我们检查一下我们仓库的当前状态:
git status
你应该会看到类似以下的输出:
On branch master
Your branch is up to date with 'origin/master'.
nothing to commit, working tree clean
这意味着你的本地仓库当前与远程仓库是同步的。
让我们在仓库中创建一个新文件:
echo "This is a new file for our project." > new_file.txt
创建文件后,我们需要将其添加到 Git 的暂存区(staging area):
git add new_file.txt
现在,我们将此文件提交到本地仓库:
git commit -m "Add new_file.txt"
你应该会看到确认提交的输出:
[master 1a2b3c4] Add new_file.txt
1 file changed, 1 insertion(+)
create mode 100644 new_file.txt
在我们进行了本地提交后,让我们再次检查一下仓库的状态:
git status
这次,你应该会看到:
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
(use "git push" to publish your local commits)
nothing to commit, working tree clean
此消息表明你的本地分支有一个尚未推送到远程仓库的提交。这正是我们想要创建的场景——你的分支现在“领先于 origin”了。
既然你的本地分支已经领先于远程分支,让我们探索如何查看本地和远程分支之间的具体差异。
在 Git 中,你使用 origin/branch-name 格式来引用远程分支。origin 是远程仓库的默认名称,而 branch-name 是分支的名称(在我们的例子中是 master)。
要查看存在于你的本地分支但不存在于远程分支中的提交(commits),请使用以下命令:
git log origin/master..HEAD
输出将向你展示存在于本地分支 (HEAD) 但不存在于远程分支 (origin/master) 中的提交:
commit 1a2b3c4d... (HEAD -> master)
Author: LabEx User <labex@example.com>
Date: ...
Add new_file.txt
要查看本地和远程分支之间哪些文件存在差异,请使用:
git diff --name-status origin/master..HEAD
输出应该显示:
A new_file.txt
这表明 new_file.txt 在你的本地分支中被添加了,但它在远程分支中不存在。
要查看实际的内容差异,你可以使用:
git diff origin/master..HEAD
这将显示对每个文件所做的具体更改:
diff --git a/new_file.txt b/new_file.txt
new file mode 100644
index 0000000..3b2aed8
--- /dev/null
+++ b/new_file.txt
@@ -0,0 +1 @@
+This is a new file for our project.
这些命令可以帮助你确切地了解本地分支中存在哪些尚未推送到远程仓库的更改。
现在你已经了解了本地分支中存在哪些更改,是时候通过推送(pushing)你的更改来同步本地仓库和远程仓库了。
git push 命令会将你的本地提交(commits)发送到远程仓库。当你执行推送时,你将远程分支与本地分支同步,使两个分支完全一致。
要将你的本地提交推送到远程仓库,请使用:
git push origin master
你应该会看到类似以下的输出:
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 2 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 324 bytes | 324.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
To https://github.com/YOUR_USERNAME/git-playground.git
abcd123..1a2b3c4 master -> master
输出显示了:
master -> master)让我们再次检查我们仓库的状态:
git status
现在你应该会看到:
On branch master
Your branch is up to date with 'origin/master'.
nothing to commit, working tree clean
这确认了你的本地分支现在已与远程分支同步。“领先于 origin”的消息消失了,因为两个分支现在包含相同的提交。
我们可以通过检查远程分支的日志来验证我们的更改现在是否已在远程仓库中:
git log origin/master -1
此命令显示远程分支上的最新提交,该提交现在应该包含我们的新文件:
commit 1a2b3c4d... (HEAD -> master, origin/master)
Author: LabEx User <labex@example.com>
Date: ...
Add new_file.txt
注意,HEAD -> master 和 origin/master 现在都指向同一个提交,确认两个分支已同步。
在实际场景中,你的本地分支可能领先于远程分支有多个提交(commits)。让我们创建这种情况,并学习如何解决它。
让我们创建并提交多个更改:
## 创建第二个文件
echo "This is the second file." > second_file.txt
git add second_file.txt
git commit -m "Add second_file.txt"
## 修改 README
echo "### Additional Information" >> README.md
echo "This project demonstrates Git branch synchronization." >> README.md
git add README.md
git commit -m "Update README with additional information"
现在我们检查一下状态:
git status
你应该会看到:
On branch master
Your branch is ahead of 'origin/master' by 2 commits.
(use "git push" to publish your local commits)
nothing to commit, working tree clean
让我们看看本地有哪些提交不在远程:
git log origin/master..HEAD --oneline
你应该会看到你的两个新提交:
abcd123 Update README with additional information
efgh456 Add second_file.txt
要将所有本地提交同步到远程仓库,请使用相同的推送命令:
git push origin master
输出将显示两个提交都被推送:
Enumerating objects: 7, done.
Counting objects: 100% (7/7), done.
Delta compression using up to 2 threads
Compressing objects: 100% (4/4), done.
Writing objects: 100% (5/5), 574 bytes | 574.00 KiB/s, done.
Total 5 (delta 1), reused 0 (delta 0), pack-reused 0
To https://github.com/YOUR_USERNAME/git-playground.git
1a2b3c4..abcd123 master -> master
让我们再次检查我们的状态:
git status
你应该会看到:
On branch master
Your branch is up to date with 'origin/master'.
nothing to commit, working tree clean
这确认了所有本地提交都已成功推送到远程仓库。
在这个实验(Lab)中,你学习了如何处理本地 Git 分支领先于远程分支的情况。你成功地完成了以下操作:
这些技能对于日常的 Git 工作流程至关重要,因为它们能帮助你保持本地和远程仓库的同步,这对于软件开发项目中有效的协作至关重要。请记住,定期保持分支同步有助于防止冲突,并使协作更加顺畅。