无法理解Git分支、合并和rebase

发布于 2024-07-22 15:02:24 字数 831 浏览 7 评论 0原文

我知道线程 表示 rebase 用于队友的小变化,而 merge 用于大变化。

我将三个队友的三个 Git 保留在以下目录结构中,其中我们都有相同的初始代码:

project 
      | - I
      | - myTeamMate1
      | - myTeamMate2

分支不在同一个 Git 中。 这意味着我无法使用变基和合并。 我使用 vimdiff 来同步队友之间的更改。 然而,这很耗时。

我没有成功地尝试创建以下目录结构,其中所有分支都在一个 Git 下:

project
      | - I - myTeamMate1 - myTeamMate2

但是,我为我和我的队友运行克隆命令:

git clone <url>

并且在为我的队友运行第二个克隆后收到错误消息

fatal: destination path 'dotFiles' already exists and is not an empty directory.

哪个目录我应该与 3 人团队一起使用结构,以便我可以使用 rebasemerge 命令吗?

I know the thread which says that rebase is for small changes of teamMates, while merge for large changes.

I keep three Gits of three teammates in the following directory structure where we all have the same initial code:

project 
      | - I
      | - myTeamMate1
      | - myTeamMate2

The branches are not in the same Git. This means that I cannot use rebase and merge.
I have used vimdiff to sync changes between teamMates. However, this is time-consuming.

I have unsuccessfully tried to make the following directory structure where all branches are under one Git:

project
      | - I - myTeamMate1 - myTeamMate2

However, I run clone command for me and for my teammate:

git clone <url>

and I get the error message after running the second clone for my teammate

fatal: destination path 'dotFiles' already exists and is not an empty directory.

Which directory structure should I use with a team of 3 people, such that I can use rebase and merge commands?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

隱形的亼 2024-07-29 15:02:24

只是一些其他的想法来完成塞缪尔的回答。

  • 与 SVN 不同,Git 中的分支和目录完全不相关:拥有 3 个分支(团队的每个成员一个)并不意味着 3 个目录。 这不是你问题中的意思(因为你的目录实际上是 3 个 Git 存储库的 3 个根),但我更喜欢明确提及它以防万一;)

  • Git 是 DVCS,这 3 个存储库可以位于任何地方(不在同一台计算机上的 3 个目录)。 如果可以通过 UNC 路径 (\\desktop\path\to\repo) 访问它们,则可以将它们指定为远程。

  • Git 克隆确实允许您获取对远程分支的引用,但不会创建跟踪本地分支,从而允许您获取同事的工作。 ruby 模块“远程分支”可以提供帮助。

  • 警惕 rebase,因为它会重写分支的 SHA-1 (因为您在另一个分支之上重放您的提交):如果您的队友基于您的分支进行合并,那么他们每次都必须合并您的所有提交,即使是那些已经合并的提交!
    在这种情况下最好有 2 个分支:

    • 您用于开发和变基的工作分支(集成同行的工作,尽管该操作也可以在自己的分支中完成)
    • 发布公共分支,您只能在其中合并稳定的工作,然后其他远程存储库可以将其用作合并源。

Just some other thoughts to complete Samuel's answer.

  • Unlike SVN, branches and directories are completely unrelated in Git: having 3 branches (one for each member of the team) does not mean 3 directories. That is not what you meant in your question (since your directories are actually 3 roots for 3 Git repositories), but I prefer mention it explicitly just in case ;)

  • Git being DVCS, the 3 repositories can be anywhere (not in 3 directories on the same computer). If they are reachable through an UNC path (\\desktop\path\to\repo), they can be designated as remote.

  • Git clone does allow you to get references to remotes branches, but does not create tracking local branches allowing you to get the work of your colleagues. The ruby module "remote branches" can help.

  • Be wary of rebase as it rewrites the SHA-1 of your branch (since you replay your commits on top of another branch): if your teammates based their merges on your branches, they will have to merge all your commits every time, even those already merged!
    It is best to have 2 branches in this case:

    • a working branch you are using for your development and rebase (to integrate the works of your peers, although even that operation could be done in its own branch)
    • a publishing public branch, on which you only merge your stable work, and which can then be used by other remote repositories as a source for merges.
羁绊已千年 2024-07-29 15:02:24

事实上,它们并不像您所说的那样位于同一个 git 内,这不会以任何方式限制您。 Git 是分布式的,这意味着您可以在这些存储库之间获取、合并、变基等。

查看git remote --help,了解如何命名您的队友存储库,以便您可以轻松地导入他们的更改,rebase到它们或执行合并。 无需更改您的目录结构,您的目录结构完全可用。

The fact that they are not inside the same git as you state doesn't limit you in any way. Git is distributed, which means that you can fetch between those repositories, merge, rebase, and so on.

Look at git remote --help to see how you can name your teammates repositories in yours so that you can easily import their changes, rebase onto them or perform merges. There is no need to change your directory structure, yours is perfectly usable.

最好是你 2024-07-29 15:02:24

代码示例

跟踪远程分支

git remote add -t master Hnr git://github.com/userName/files.git

这不会下载分支。 它只是编辑文件 .git/config 并添加几行告诉 Git 如何从何处以及如何远程获取数据。

要获取您的队友的分支,请运行

git fetch Hnr

要查看您是否确实拥有您的队友的分支,请运行

git branch -a

或仅查看您朋友的分支

git branch -r

上述两个命令最初引起了我的困惑,因为我不知道它们。 这让我尝试其他不必要的命令来为我创建分支。

拥有你朋友的分支的第二种方法

但是,请注意,你可以使用 git-clone 将你朋友的代码复制到一个单独的目录,如下所示 然后

git clone UrlAtGithub

,你显然可以像上面一样使用 Git-remote 来拥有一个分支在您的 Git 存储库中:

git remote add master Hnr PATH/ToYourFriendRepo.git

结论:您可以使用两种方式来获取您的 teamMate 分支:通过 git-clone 或不使用 git-clone。 这两种方式都需要使用git remote add。 后者的优点似乎是您需要少运行一个命令。 前者再次将队友的整个存储库提供给您的硬盘。

请参阅 Git 手册了解如何更新远程分支。 (注意远程分支可以位于您的计算机上。它不必位于远程计算机中。您的分支也可以是远程分支,但这会限制您的工作流程。)


接下来您可能想要合并您朋友的特定文件到您的分支机构。
在这种情况下,知道您朋友提交的前 5 个字母就足以合并文件。

[我不知道你如何执行以下操作:] 你需要运行,例如

git merge 76a32

你可能会得到以下输出

CONFLICT (add/add): Merge conflict in shells/externalPrograms
Auto-merging shells/smallApps/extract
CONFLICT (add/add): Merge conflict in shells/smallApps/extract
Auto-merging shells/smallApps/extract~
CONFLICT (add/add): Merge conflict in shells/smallApps/extract~
Automatic merge failed; fix conflicts and then commit the result.

在这种情况下,你的文件与你朋友的文件有很大不同,Git 无法决定哪个要采取的文件。 显然,您需要运行以下命令

git rebase

,然后得到

shells/smallApps/extract: needs merge
shells/smallApps/extract~: needs merge
cannot rebase: you have unstaged changes

练习 1: 但是,我现在陷入困境,因为我的 tig 没有显示我朋友在我的 Git 树上的提交。
如何将您朋友的提交 76a32 获取到您的 Git 树您的 Git 中的哪个分支?


您现在可能已经解决了问题。 问题在于我忽略了解决 Git 给我通知的冲突。 Google 在Git 官方网站上再次扭转局面:

如果存在冲突——例如,
如果同一个文件被修改两次
远程分支中的不同方式
和当地分行——那么你就是
警告--

下一个问题是清理有问题的部分并再次合并。
您在有问题的文件中看到类似于以下内容的内容 - 玩得开心! (我终于遇到真正的问题了;)

alt text http://dl.getdropbox.com /u/175564/exampleGitProblemSolving.png


在 Mac 的 FileMerge 中比较和合并 dotFiles 时出现问题

我需要更新我的 Git,因为只有最新的 Git 才有 difftool -命令。 您可以在线程。

Difftool 允许您从终端启动 FileMerge 中的 dotFiles。 您无法在 GUI 中访问它们。 它的工作原理类似于

git difftool master:README dev:README

添加新的远程帐户后您在 Github 中的帐户出现问题

您的 Github 帐户可能已在 .git/config 中消失。 在这种情况下,您需要运行以下代码

git remote add -f YourUserName [email protected]

然后您可能会注意到您无法正常 git-push

git push origin master

您可能会收到以下错误消息

fatal: 'origin' does not appear to be a git repository
fatal: The remote end hung up unexpectedly

您还可以尝试以下命令来解决问题

git push Masi master

以及 Masi 的 2 个排列下的所有 3 个,主人和起源。
然而,没有一个命令不起作用。

练习2当您的远程列表中有 teamMate 时,如何提交到您的 github 帐户?

Origin 是实际外部 git-repo 的简称,例如 Github 上的。
您的变量源的内容可能已被另一个远程存储库替换。
在这种情况下,我建议您通过注意创建一个新变量

git remote add github [email protected]:myLogin/myProject.git

,您可以在 github 的位置使用 origin2 。 Origin 只是存储库命名的约定。

然后你可以简单地运行

git push github master

You may have a passphrase in your ssh-key。 如果有,您可能会收到权限被拒绝的警告。 如果您这样做了,请参阅线程

Code examples

To track a remote branch

git remote add -t master Hnr git://github.com/userName/files.git

This does not download you the branch. It just edits the file .git/config and adds a few lines which tell Git how where and how to get data remotely.

To get your teamMate's branch, run

git fetch Hnr

To see that you really have your teamMates' branch, run

git branch -a

or to see just your friends' branches

git branch -r

The above two commands caused my confusion initially, since I did not know them. This made me try other unnecessary commands to have the branches for me.

2nd way to have your friend's branch

However, note that you can use git-clone to have your friend's code to a separate directory as follows

git clone UrlAtGithub

Then, you can apparently use Git-remote as above to have a branch at your Git repo:

git remote add master Hnr PATH/ToYourFriendRepo.git

Conclusion: You can use two ways to have your teamMate's branch for you, either by git-clone or without it. Both ways require the use of git remote add. The advantage of the latter seems to be that you need to run one command less. The former then again gives you your teamMates' whole repo to your hardDisk.

Please, see Git's manuals for how you can update your remote branches. (NB remote branch can be at your computer. It does not have to be in a remote computer. Your branch can be the remote branch too, but this restricts your workflow.)


You may next want to merge your friend's specific file to your branch.
In that case, it is enough to know the 5 first letters of your friend's commit to merge the file.

[I am not sure how you can do the following:] You need to run, for instance

git merge 76a32

You may get the following output

CONFLICT (add/add): Merge conflict in shells/externalPrograms
Auto-merging shells/smallApps/extract
CONFLICT (add/add): Merge conflict in shells/smallApps/extract
Auto-merging shells/smallApps/extract~
CONFLICT (add/add): Merge conflict in shells/smallApps/extract~
Automatic merge failed; fix conflicts and then commit the result.

In that case, your files differ significantly from your friend's ones and Git cannot decide which file to take. You need to run apparently the following

git rebase

and you get

shells/smallApps/extract: needs merge
shells/smallApps/extract~: needs merge
cannot rebase: you have unstaged changes

Exercise 1 : However, I am now in stuck, since my tig does not show my friend's commit at my Git tree.
How can you get your friend's commit 76a32 to your Git tree which branch is in your Git?


You may now have solved the problem. The problem was in that I ignored to solve the conflicts about which Git gave me notifications. Google saves the day again at the official Git site:

If there are conflicts--for example,
if the same file is modified in two
different ways in the remote branch
and the local branch--then you are
warned --

The next problem is to clean up the problematic parts and merge again.
You see something similar to the following in your problematic files -- have fun! (I finally get to real problems ;)

alt text http://dl.getdropbox.com/u/175564/exampleGitProblemSolving.png


Problem in diffing and merging dotFiles in Mac's FileMerge

I needed to update my Git, since only the newest Git has difftool -command. You can find a solution at the thread.

Difftool allows you to start dotFiles in FileMerge from terminal. You cannot access them in GUI. It works like

git difftool master:README dev:README

Problem with your account in Github after adding a new remote account

Your Github account may have been disappeared in .git/config. In that case, you need to run the following code

git remote add -f YourUserName [email protected]

You then may notice that you cannot git-push normally by

git push origin master

You may get the following error message

fatal: 'origin' does not appear to be a git repository
fatal: The remote end hung up unexpectedly

You may also try the following commands to solve the problem

git push Masi master

and all 3 under 2 permutations of Masi, master and origin.
However, none of the commands did not work.

Exercise 2: How can you commit to your github account as you have a teamMate in your remote list?

Origin is a shortname for your actual external git-repo, for instance, at Github.
The content of your variable origin may have been replaced by another remote repo.
In this case, I recommend you to create a new variable by

git remote add github [email protected]:myLogin/myProject.git

Note that you could have origin2 in the place of github. Origin is just a convention in naming the repo.

You can then simply run

git push github master

You may have a passphrase in your ssh-key. If you have, you may get a permission denied -warning. If you did, please see the thread.

说不完的你爱 2024-07-29 15:02:24

说实话,我开始努力输入一个漂亮、长、详细的回复......但我已经比我在 http://excess.org/article/2008/07/ogre-git-tutorial/。 他介绍了不同分支、远程和合并的使用。 它也是通过截屏视频完成的,所以请花一个小时,喝杯咖啡,享受讲座。

具体到你的问题:你不应该使用目录设置。 Git 处理目录分支的方式与 SVN 处理分支文件夹的方式不同。 git 中的分支是完全不同的事情。 虽然您可能不设置中央存储库(我推荐它),但您应该设置一个 ma​​ster 分支,每个人都将其最终的、干净的工作提交到其中。

我的一个示例存储库在 Wikimedia Commons 上有一个屏幕截图。 在我的示例中,“调酒师”仅以 tar 块的形式提交代码,因此我必须自己导入它来管理它。 “master”(此处仅显示为remotes/elf/master)是一个公共svn存储库,我从中导入并进行基础更改。 “专制”是我自己的代码基线,“私有”是我保存配置文件的位置,其中包含我想要管理但从未发布的密码。 检查私有的时间戳,您会发现它们与下面的分支不一致。 这是因为我将分支重新定位为“独裁”而不是合并

您会注意到两个远程引用:elf 和 bard。 这是我的两台托管该软件的远程计算机。 我可以按照自己的意愿将树枝推拉到那里。 就您而言,您将拥有其他开发人员的远程存储库,并且他们会修改自己的分支。 观看教程将使您更好地了解它是如何工作的。 祝你好运 :)

Truth be told, I started to work on typing a nice, long, detailed response... but I this is already explained much better than I could at http://excess.org/article/2008/07/ogre-git-tutorial/. He covers the use of different branches, remotes, and merging. It's also done in screencast, so take an hour, grab some coffee, and enjoy the lecture.

Specific to your question: you shouldn't use a directory setup. Git doesn't handle branching with directories in the same way that SVN does with branch folders. Branches in git are an entirely different thing. While you may not setup a central repository (I recommend it), you should setup a single master branch that everybody commits their final, clean work to.

One example repository of mine has a screenshot at Wikimedia Commons. In my example, "bartender" submits his code only in tar chunks, so I have to import it myself to manage it. "master" (shown here only as remotes/elf/master) is a public svn repository that I import and base changes off of. "autocracy" is my own baseline for the code, and "private" is where I keep a config file with passwords that I want to manage but never publish. Check the timestamps on private, and you'll notice they don't line up with the branches below. This is because I rebase that branch onto "autocracy" instead of merging.

You'll notice two remote references: elf and bard. These are two remote machines of mine that host the software. I can push and pull my branches to there as I wish. In your case, you'd have remote repositories of your other developers, and they'd modify their own branches. Watching the tutorial will give you a better idea of how it works. Good luck :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文