可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
When you run git pull
on the master
branch, it typically pulls from origin/master
. I am in a different branch called newbranch
, but I need to run a command that does a git pull
from origin/master
into master
but I cannot run git checkout
to change the selected branch until after the pull is complete. Is there a way to do this?
To give some background, the repository stores a website. I have made some changes in newbranch
and deployed them by switching the website to newbranch
. Now those changes have been merged upstream into the master
branch, I am trying to switch the website back to the master
branch as well. At this point, newbranch
and origin/master
are identical, but master
is lagging behind origin/master
and needs to be updated. The problem is, if I do it the traditional way:
$ git checkout master
# Uh oh, production website has now reverted back to old version in master
$ git pull
# Website is now up to date again
I need to achieve the same as above (git checkout master && git pull
), but without changing the working directory to an earlier revision during the process.
回答1:
You've got a worktree you don't want to touch, so use another one. Clone is cheap, it's built for this.
git fetch origin master # nice linear tree
git clone . ../wip -b master # wip's `origin/master` is my `master`
cd ../wip # .
git pull origin origin/master # merge origin's origin/master
git push origin master # job's done, turn it in.
cd ../main
rm -rf ../wip # wip was pushed here, wip's done
git checkout master # payload
The problem with all the other answers here is, they don't actually do the pull. If you need the merge or rebase you've got pull set up for, you need another worktree and the above procedure. Otherwise just git fetch; git checkout -B master origin/master
will do.
回答2:
Straightforward: Updating from a remote branch into a currently not checked-out branch master:
git fetch origin master:master
where origin is your remote and you are currently checked out in some branch e.g. dev.
If you want to update your current branch in addition to the specified branch at one go:
git pull origin master:master
回答3:
This is answered here:
Merge, update, and pull Git branches without using checkouts
# Merge local branch foo into local branch master,
# without having to checkout master first.
# Here `.` means to use the local repository as the "remote":
git fetch . foo:master
# Merge remote branch origin/foo into local branch foo,
# without having to checkout foo first:
git fetch origin foo:foo
回答4:
As it turns out, the answer is deceptively simple:
$ git fetch # Update without changing any files
$ git branch -d master # Remove out-of-date 'master' branch
$ git checkout --track origin/master # Create and check out up-to-date 'master' branch
This allows you to update the master
branch without switching to it until after it has been updated.
回答5:
You're worried about something that cannot be fixed, as Git operations are not atomic. You will always have a hole where your working directory is half way between branches, even if you update master without first switching to it. This is why Git is not a deployment tool.
Since you're not actually committing code in your production environment (I hope), you don't actually need to have a branch checked out. You can simply do a git fetch
to update your remote refs, and then git checkout origin/master
to move the working directory directly to the commit currently pointed to by origin/master
. This will put you in a detached head state, but again, as you're not committing code, this doesn't matter.
This is the smallest hole you're going to get, but as I said, a hole still exists; checkout
is not atomic.
回答6:
You can use update-ref for this:
git fetch
git update-ref refs/heads/master origin/master
git checkout master
Note that this would throw away any local commits in the master branch. In your case there won't be any so this is okay. For other people trying to do this where there are local commits, I don't think it's possible, since merge can only be run on the current branch.
回答7:
Malvineous's solution work for me
$ git fetch # Update without changing any files
$ git branch -d master # Remove out-of-date 'master' branch
$ git checkout --track origin/master # Create and check out up-to-date 'master' branch
Just in give the error
warning: not deleting branch 'master' that is not yet merged to
'refs/remotes/origin/master', even though it is merged to HEAD.
error: The branch 'master' is not fully merged.
If you are sure you want to delete it, run 'git branch -D master'.
So i run with -D option
thanks