如何在不进行获取的情况下预览 git-pull?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/180272/
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
How to preview git-pull without doing fetch?
提问by Milan Babu?kov
Is it even possible?
甚至有可能吗?
Basically, there's a remote repository from which I pull using just:
基本上,有一个远程存储库,我只使用:
git pull
Now, I'd like to preview what this pull would change (a diff) without touching anything on my side. The reason is that thing I'm pulling might not be "good" and I want someone else to fix it before making my repository "dirty".
现在,我想在不触及我身边的任何东西的情况下预览这个拉动会改变什么(差异)。原因是我拉的东西可能不是“好”的,我希望其他人在使我的存储库“脏”之前修复它。
回答by Greg Hewgill
After doing a git fetch
, do a git log HEAD..origin/master
to show the log entries between your last common commit and the origin's master branch. To show the diffs, use either git log -p HEAD..origin/master
to show each patch, or git diff HEAD...origin/master
(three dots not two) to show a single diff.
执行 a 后git fetch
,执行 agit log HEAD..origin/master
以显示上次常见提交和源的主分支之间的日志条目。要显示差异,请使用要么git log -p HEAD..origin/master
显示每个补丁,要么git diff HEAD...origin/master
(三个点而不是两个)显示一个差异。
There normally isn't any need to undo a fetch, because doing a fetch only updates the remotebranches and none of yourbranches. If you're not prepared to do a pull and merge in all the remote commits, you can use git cherry-pick
to accept only the specific remote commits you want. Later, when you're ready to get everything, a git pull
will merge in the rest of the commits.
通常不需要撤消提取,因为执行提取只会更新远程分支,而不会更新您的任何分支。如果您不准备在所有远程提交中进行拉取和合并,您可以使用git cherry-pick
仅接受您想要的特定远程提交。稍后,当您准备好获取所有内容时, agit pull
将合并到其余的提交中。
Update: I'm not entirely sure why you want to avoid the use of git fetch. All git fetch does is update your local copy of the remote branches. This local copy doesn't have anything to do with any of your branches, and it doesn't have anything to do with uncommitted local changes. I have heard of people who run git fetch in a cron job because it's so safe. (I wouldn't normally recommend doing that, though.)
更新:我不完全确定您为什么要避免使用 git fetch。git fetch 所做的只是更新远程分支的本地副本。这个本地副本与您的任何分支都没有任何关系,也与未提交的本地更改没有任何关系。我听说有人在 cron 作业中运行 git fetch,因为它非常安全。(不过,我通常不建议这样做。)
回答by Brian Gianforcaro
I think git fetchis what your looking for.
我认为git fetch是你要找的。
It will pull the changes and objects without committing them to your local repo's index.
它将拉取更改和对象,而无需将它们提交到本地存储库的索引。
They can be merged later with git merge.
它们可以稍后与git merge 合并。
Edit: Further Explination
编辑:进一步解释
Straight from the Git- SVN Crash Courselink
直接来自Git-SVN 速成课程链接
Now, how do you get any new changes from a remote repository? You fetch them:
git fetch http://host.xz/path/to/repo.git/
At this point they are in your repository and you can examine them using:
git log origin
You can also diff the changes. You can also use git log HEAD..originto see just the changes you don't have in your branch. Then if would like to merge them - just do:
git merge origin
Note that if you don't specify a branch to fetch, it will conveniently default to the tracking remote.
现在,您如何从远程存储库获取任何新更改?你去取它们:
git fetch http://host.xz/path/to/repo.git/
此时它们在您的存储库中,您可以使用以下方法检查它们:
git log origin
您还可以区分更改。您还可以使用git log HEAD..origin来查看您的分支中没有的更改。然后,如果想合并它们 - 只需执行以下操作:
git merge origin
请注意,如果您不指定要获取的分支,它将方便地默认为跟踪远程。
Reading the man page is honestly going to give you the best understanding of options and how to use it.
老实说,阅读手册页会让您对选项以及如何使用它有最好的理解。
I'm just trying to do this by examples and memory, I don't currently have a box to test out on. You should look at:
我只是想通过例子和记忆来做到这一点,我目前没有一个盒子来测试。你应该看看:
git log -p //log with diff
A fetch can be undone with git reset --hard(link) , however all uncommitted changes in your tree will be lost as well as the changes you've fetched.
可以使用git reset --hard( link)撤消获取,但是树中所有未提交的更改以及已获取的更改都将丢失。
回答by Antonio Bardazzi
You can fetch from a remote repo, see the differences and then pull or merge.
您可以从远程存储库中获取,查看差异,然后拉取或合并。
This is an example for a remote repo called origin
and a branch called master
tracking the remote branch origin/master
:
这是一个名为远程存储库origin
和一个名为master
跟踪远程分支的分支的示例origin/master
:
git checkout master
git fetch
git diff origin/master
git pull --rebase origin master
回答by Matthias
I created a custom git alias to do that for me:
我创建了一个自定义 git 别名来为我做到这一点:
alias.changes=!git log --name-status HEAD..
with that you can do this:
你可以这样做:
$git fetch
$git changes origin
This will get you a nice and easy way to preview changes before doing a merge
.
这将为您提供一种在执行merge
.
回答by Andy P
I may be late to the party, but this is something which bugged me for too long. In my experience, I would rather want to see which changes are pending than update my working copy and deal with those changes.
我可能会迟到,但这件事困扰了我太久。根据我的经验,我宁愿查看哪些更改未决,也不愿更新我的工作副本并处理这些更改。
This goes in the ~/.gitconfig
file:
这在~/.gitconfig
文件中:
[alias]
diffpull=!git fetch && git diff HEAD..@{u}
It fetches the current branch, then does a diff between the working copy and this fetched branch. So you should only see the changes that would come with git pull
.
它获取当前分支,然后在工作副本和这个获取的分支之间做一个差异。因此,您应该只看到git pull
.
回答by cowboycb
I use these two commands and I can see the files to change.
我使用这两个命令,我可以看到要更改的文件。
First executing git fetch, it gives output like this (part of output):
... 72f8433..c8af041 develop -> origin/develop ...
首先执行git fetch,它给出这样的输出(输出的一部分):
... 72f8433..c8af041 develop -> origin/develop ...
This operation gives us two commit IDs, first is the old one, and second will be the new.
这个操作给了我们两个提交 ID,第一个是旧的,第二个是新的。
Then compare these two commits using git diff
git diff 72f8433..c8af041 | grep "diff --git"
然后使用git diff比较这两个提交
git diff 72f8433..c8af041 | grep "diff --git"
This command will list the files that will be updated:
此命令将列出将要更新的文件:
diff --git a/app/controller/xxxx.php b/app/controller/xxxx.php
diff --git a/app/view/yyyy.php b/app/view/yyyy.php
For example app/controller/xxxx.phpand app/view/yyyy.phpwill be updated.
例如app/controller/xxxx.php和app/view/yyyy.php将被更新。
Comparing two commits using git diffprints all updated files with changed lines, but with grepit searches and gets only the lines contains diff --gitfrom output.
使用git diff比较两个提交会打印所有带有更改行的更新文件,但使用grep它会搜索并仅从输出中获取包含diff --git的行。
回答by AXE Labs
If you don't want git-fetchto update your local .git, just copy your local repo to a temp dir and do a pull there. Here is a shor-hand:
如果您不希望git-fetch更新您的本地 .git,只需将您的本地存储库复制到临时目录并在那里执行拉取操作。这是一个短手:
$ alias gtp="tar -c . | (cd /tmp && mkdir tp && cd tp && tar -x && git pull; rm -rf /tmp/tp)"
Ex.:
前任。:
$ git status
# On branch master
nothing to commit (working directory clean)
$ gtp
remote: Finding sources: 100% (25/25)
remote: Total 25 (delta 10), reused 25 (delta 10)
Unpacking objects: 100% (25/25), done.
From ssh://my.git.domain/reapO
32d61dc..05287d6 master -> origin/master
Updating 32d61dc..05287d6
Fast-forward
subdir/some.file | 2 +-
.../somepath/by.tes | 3 ++-
.../somepath/data | 11 +++++++++++
3 files changed, 14 insertions(+), 2 deletions(-)
$ git status
# On branch master
nothing to commit (working directory clean)
$ git fetch
remote: Finding sources: 100% (25/25)
remote: Total 25 (delta 10), reused 25 (delta 10)
Unpacking objects: 100% (25/25), done.
From ssh://my.git.domain/reapO
32d61dc..05287d6 master -> origin/master
$ git status
# On branch master
# Your branch is behind 'origin/master' by 3 commits, and can be fast-forwarded.
#
nothing to commit (working directory clean)
回答by Ed Greenberg
What about cloning the repo elsewhere, and doing git log on both the real checkout and the fresh clone to see if you got the same thing.
如何在别处克隆 repo,并在真实结帐和新克隆上执行 git log 以查看是否得到相同的东西。