理解 git fetch 然后合并

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3419658/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 08:51:53  来源:igfitidea点击:

understanding git fetch then merge

git

提问by Hans

Coming from an svn background, I had this question:

来自 svn 背景,我有这个问题:

git equivalent of svn status -u

git 相当于 svn status -u

(what is the git equivalent of svn status -u)

(git相当于什么svn status -u

And I understand, you do:

我明白,你这样做:

git fetch
git log ..origin/master

But, I'm guessing the origin/masterpart depends on the branch? It wouldn't necessarily be master if I was tracking a remote branch?

但是,我猜这origin/master部分取决于分支?如果我正在跟踪远程分支,它不一定是 master 吗?

I also don't understand the git merge origin/masterprecisely. I'm guessing that just means that git fetchgrabbed the changes from the remote and put them into the git database system as origin/master and I'm in just master? What if I fetched changes, check what was done, am horrified by the changes and don't want to merge? How do I basically abandon them?

我也不是很清楚git merge origin/master。我猜这只是意味着git fetch从远程获取更改并将它们作为 origin/master 放入 git 数据库系统中,而我只是在 master 中?如果我获取了更改,检查做了什么,对更改感到震惊并且不想合并怎么办?我如何基本上放弃它们?

回答by Jakub Nar?bski

git fetch

混帐

git fetchgrabs changes from remote repository and puts it in your repository's object database. It also fetches branches from remote repository and stores them as remote-tracking branches.

git fetch从远程存储库获取更改并将其放入存储库的对象数据库中。它还从远程存储库中获取分支并将它们存储为远程跟踪分支

When you are fetching git tells you where it stores each branch on remote repository it fetches. For example you should see something like

当您获取 git 时,它会告诉您它将获取的远程存储库中的每个分支存储在哪里。例如,你应该看到类似

   7987baa..2086e7b  master -> origin/master

when fetching. This means that 'origin/master' stores where 'master' is on 'origin' repository.

取货时。这意味着“origin/master”存储“master”在“origin”存储库中的位置。

If you examine .git/configfile, you would see the following fragment:

如果您检查.git/config文件,您会看到以下片段:

[remote "origin"]
        url = git://git.example.com/repo.git
        fetch = +refs/heads/*:refs/remotes/origin/*

This (among others) means that any branch 'A' ('refs/heads/A') in origin remote (repository you cloned from) would be saved as 'origin/A' ('refs/remotes/origin/A').

这(除其他外)意味着 origin remote(您从中克隆的存储库)中的任何分支 'A' ('refs/heads/A') 将被保存为 'origin/A' ('refs/remotes/origin/A') .

git log ..origin/master

git log ..origin/master

As you can see 'origin/master' is 'master' in origin. If you are on (default) 'master' branch, then git log ..origin/master, which is equivalent to git log HEAD..origin/master, which when on 'master' branch is equivalent to git log master..origin/masterwould list all commits that are on 'master' branch in remote repository and are not in local 'master' branch where you do your work.

正如你所看到的,'origin/master' 是 'master' 的起源。如果您在(默认)'master' 分支上,则git log ..origin/master, 相当于git log HEAD..origin/master,当在 'master' 分支上相当于时,git log master..origin/master将列出远程存储库中 'master' 分支上且不在本地 'master 中的所有提交' 你工作的分支。

The more generic version in modern git (assuming that upstream / tracking information exists) would be to use simply

现代 git 中更通用的版本(假设存在上游/跟踪信息)将简单地使用

$ git log ..@{u}

(Here @{u}is synonym for @{upstream}, see gitrevisionsmanpage).

(这@{u}是 的同义词@{upstream},请参阅gitrevisions联机帮助页)。

git merge origin/master

git合并原点/主

git mergeis used to join two lines of history. If one of sides didn't do any work since last branching point (since merge base), the situation is either fast-forward(the branch you are on is simply updated to the tip of the branch you are merging), or up-to-date(there is nothing new to merge, and the branch you are on stays unchanged).

git merge用于连接两行历史记录。如果一方自上一个分支点以来没有做任何工作(自合并基础以来),则情况要么快进(您所在的分支只是更新到您正在合并的分支的尖端),要么向上 -迄今为止(没有任何新内容要合并,并且您所在的分支保持不变)。

git fetchfollowed by git merge origin/master, when on 'master' branch, is equivalent to issuing

git fetch其次是git merge origin/master,当在'master'分支上时,相当于发出

$ git pull


If you don't want to merge, you don't need to. Note that you can use e.g. git reset --hard HEAD@{1}to go back and discard result of git pullif you don't like it.

如果您不想合并,则无需合并。请注意,如果您不喜欢它,您可以使用 eggit reset --hard HEAD@{1}返回并丢弃结果git pull

回答by strager

git fetchdownloads all the changes needed to represent the given remote branch. Typically this is origin/masteror similar.

git fetch下载代表给定远程分支所需的所有更改。通常这是origin/master或类似的。

git mergemerges two branches together by creating new commits or fast-forwarding (or a combination). It doesn't change any commits you have made, and you can always roll back to your old branch (using git resetor git checkout).

git merge通过创建新提交或快进(或组合)将两个分支合并在一起。它不会更改您所做的任何提交,并且您始终可以回滚到旧分支(使用git resetgit checkout)。

Note that git pullis git fetchfollowed by git merge(or git rebaseif --rebaseis given).

需要注意的是git pullgit fetch后面git merge(或者git rebase如果--rebase给出)。