本文最后更新于 2024-03-26,本文发布时间距今超过 90 天, 文章内容可能已经过时。最新内容请以官方内容为准

Git branches synchronization

To synchronize branches in Git, you can use the following steps:

  1. Ensure that you are on the branch you want to synchronize.
  2. Fetch the latest changes from the remote repository.
  3. Merge the changes from the remote branch into your local branch.
  4. Push the changes to the remote repository.

要使你的 dev 分支与最新的 main 分支保持一致,你需要在你的本地仓库中执行以下步骤:

  1. 切换到 main 分支
    首先,确保你当前不在 dev 分支上,然后在本地仓库中切换到 main 分支。

    git checkout main
    
  2. 拉取最新的 main 分支
    拉取远程仓库中的最新 main 分支,以获取最新的代码。

    git pull origin main
    

    如果在这个过程中遇到冲突,你需要手动解决这些冲突,然后提交合并后的结果。

  3. 切换回 dev 分支
    切换回你的 dev 分支。

    git checkout dev
    
  4. 合并 main 分支到 dev 分支
    现在,将最新的 main 分支合并到你的 dev 分支中。

    git merge main
    

    同样,如果合并过程中出现冲突,你需要解决这些冲突,然后提交合并后的结果。

  5. 解决冲突
    如果在上述任何步骤中遇到冲突,你需要打开涉及冲突的文件,查找以 <<<<<<<=======>>>>>>> 标记的区域,手动编辑文件以保留你想要保留的更改,并删除这些标记。然后,你可以添加并提交这些文件以解决冲突。

    git add <冲突的文件>
    git commit -m "Resolved merge conflict in <文件名>"
    

现在,你的 dev 分支应该与最新的 main 分支保持一致了。

如果你又添加了新的代码,并想要提交到 dev 分支,你可以按以下步骤操作:

  1. 添加新的代码到暂存区
    使用 git add 命令将新的代码文件添加到暂存区。

    git add <新的文件或修改的文件>
    
  2. 提交代码
    使用 git commit 命令提交这些更改。

    git commit -m "Added new features or bug fixes"
    
  3. 推送代码到远程仓库
    如果你想要将这些更改推送到远程仓库的 dev 分支,使用 git push 命令。

    git push origin dev
    

这样,你就成功地将新的代码提交到了 dev 分支,并且保持了 dev 分支与 main 分支的一致性。