git 如何检出远程/跟踪分支的 HEAD 版本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1501149/
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 can I checkout out the HEAD version of my remote/tracking branch
提问by hap497
In git, how can I checkout out the HEAD version of my remote/tracking branch? Basically, I want to do a 'svn checkout ' in git.
在 git 中,如何检出远程/跟踪分支的 HEAD 版本?基本上,我想在 git 中做一个“svn checkout”。
I think the closest thing I find is 'git fetch', but from the man page, I don't know how to checkout 1 particular file using that?
我认为我找到的最接近的东西是“git fetch”,但是从手册页中,我不知道如何使用它检出 1 个特定文件?
回答by Jakub Nar?bski
First, a note: remote repository can have more than one branch; also HEAD means (for local development and local branches) currently checked out branch, and for remote the remote-tracking branch (symbolic reference) remote/HEAD
means default branch on remote. There isn't such thing like 'HEAD of branch'; HEAD is a pointer to branch (or sometimes to a commit).
一、注意:远程仓库可以有多个分支;HEAD 也意味着(对于本地开发和本地分支)当前签出的分支,对于远程,远程跟踪分支(符号引用)remote/HEAD
意味着远程的默认分支。没有像'HEAD of branch'这样的东西;HEAD 是指向分支(或有时指向提交)的指针。
Second, Git operates (as Novelocrat wrote) on whole tree level (all files in repository). In Git you checkout a branch (although you can checkout a file version from some branch to working directory too, it is less commkon operation).
其次,Git在整个树级别(存储库中的所有文件)上运行(如Novelocrat 所写)。在 Git 中,您可以检出一个分支(虽然您也可以将某个文件版本从某个分支检出到工作目录,但它不太常见的操作)。
In Git you can create new commit only on top of some local branch. You can't create commits directly on remote-tracking branches, as they are meant to follow branches of remote repository (and you would lose your work on fetch).
在 Git 中,您只能在某些本地分支之上创建新提交。您不能直接在远程跟踪分支上创建提交,因为它们旨在跟踪远程存储库的分支(并且您会丢失获取的工作)。
Therefore simple "git checkout origin
" (assuming that remote is called origin
), which is shortcut for "git checkout origin/HEAD
", which is usually "git checkout origin/master
" would checkout a state of remote-tracking branchinto unnamed branch, so called detached HEAD. This is a good solution if you only want to see / browse remote-tracking branch state.
因此,简单的“ git checkout origin
”(假设调用了远程origin
),这是“ git checkout origin/HEAD
”的快捷方式,通常是“ git checkout origin/master
”,会将远程跟踪分支的状态检出到未命名分支,因此称为分离的 HEAD。如果您只想查看/浏览远程跟踪分支状态,这是一个很好的解决方案。
If you use "git checkout --track origin/master
" would create local branch master
, which is meant to follow (track) remote-tracking branchorigin/master
. Note that git-clone automatically sets up such local branch (usually master
) for the default branch (origin/HEAD
) of remote origin
(usually origin/master
), so you wouldn't have to do this. Then simple "git pull
" when on branch master
would fetch (if needed), and try to merge your local changes with the changes in respective branch in remote repository. If there were no changes in remote, it is "up to date" state and your local branch would not change; if there were no changes in your local branch but were in remote, then local branch is simply advanced to the state of remote-tracking branch, which is called "fast-forward". You can think of "git pull
" here as of very, very rough equivalent of "svn update
".
如果您使用“ git checkout --track origin/master
”将创建本地分支master
,这意味着跟随(跟踪)远程跟踪分支origin/master
。请注意,git-clone 会自动为远程(通常)master
的默认分支 ( origin/HEAD
)设置这样的本地分支origin
(通常origin/master
),因此您不必这样做。然后简单的“ git pull
” when on branchmaster
将获取(如果需要),并尝试将本地更改与远程存储库中相应分支中的更改合并。如果远程没有变化,它是“最新”状态,你的本地分支不会改变;如果你的本地分支没有变化,但在远程,那么本地分支只是前进到远程跟踪分支的状态,这被称为“快进”。git pull
“这里非常非常粗略地相当于“ svn update
”。
You can also force your local branch to rewind to state of remote-tracking branchwith "git reset --hard origin
". Note that --hard
means here to force overwrite of working directory, so any uncomitted changes would be lost! Use with care.
您还可以使用“ ”强制本地分支回退到远程跟踪分支的状态git reset --hard origin
。请注意,--hard
这里的意思是强制覆盖工作目录,因此任何未提交的更改都将丢失!小心使用。
Finally if you want to check out single file from remote-tracking branch(i.e. to have in working directory version of file as it is in remote-tracking branch), you can simply use the 'pathspec' form of git-checkout, namely "git checkout origin -- file
". If you want to only see how the file looks like in remote-tracking branch, use "git show origin:path/to/file
".
最后,如果您想从远程跟踪分支检出单个文件(即在远程跟踪分支中拥有文件的工作目录版本),您可以简单地使用 git-checkout 的“pathspec”形式,即“ git checkout origin -- file
”。如果您只想查看文件在远程跟踪分支中的样子,请使用“ git show origin:path/to/file
”。
HTH
HTH
回答by Phil Miller
Git doesn't have a notion of retrieving a single file. It always moves commits across the network.
Git 没有检索单个文件的概念。它总是在网络上移动提交。
Once you've done a git clone
of some remote repository, you can git checkout remotename/branch
to have the latest file versions on that branch checked out in your working copy. If you want to modify them, you should git checkout -b <yourbranchname> remotename/branch
to start a local branch. This might be better done as
一旦你完成了git clone
一些远程存储库,你可以git checkout remotename/branch
在你的工作副本中检出该分支上的最新文件版本。如果你想修改它们,你应该git checkout -b <yourbranchname> remotename/branch
启动一个本地分支。这样做可能更好
git branch --track mybranchname remotename/branchname
git checkout mybranchname
So that your local branch will "track" the remote branch, and default to pushing and pulling from that branch.
这样您的本地分支将“跟踪”远程分支,并默认从该分支推送和拉取。
回答by allenyllee
Sometimes you may suffer from detached HEAD
problems:
有时您可能会遇到以下detached HEAD
问题:
$ git checkout origin/master
Note: checking out 'origin/master'.
You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits
you make in this
state without impacting any branches by performing another checkout.
If you want to create a new branch to retain commits you
create, you may
do so (now or later) by using -b with the checkout command again. Example:
git checkout -b <new-branch-name>
HEAD is now at c3ff60a rename
Then list all branches, you will see:
然后列出所有分支,你会看到:
$ git branch -a
* (HEAD detached at origin/master)
master
remotes/origin/HEAD -> origin/master
remotes/origin/master
If you want to checkout the branch which referenced by remotes/origin/HEAD, that is remotes/origin/HEAD -> origin/master
, you can do:
如果要检查 remotes/origin/HEAD 引用的分支,即remotes/origin/HEAD -> origin/master
,您可以执行以下操作:
HEAD_BRANCH="$(git branch -r | grep 'HEAD')"
HEAD_BRANCH="${HEAD_BRANCH#*origin/HEAD -> *origin/}"
git checkout "$HEAD_BRANCH"
So that you can checkout "HEAD" version of remote/tracking branch
这样您就可以签出远程/跟踪分支的“HEAD”版本