Git:如何在提交之间来回移动
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2121230/
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
Git: How to move back and forth between commits
提问by Ondra ?i?ka
I have a newbie question about Git:
我有一个关于 Git 的新手问题:
I need to move back and forth in a history of a branch. That means, I need to get all the files to the state they were in in some old revision, and then I need to get back to the latest state in the repository. I don't need to commit.
我需要在一个分支的历史中来回移动。这意味着,我需要将所有文件恢复到它们在某个旧版本中的状态,然后我需要恢复到存储库中的最新状态。我不需要承诺。
With SVN, it would be
使用 SVN,它将是
svn up -r800
to get to revision 800, and
进入修订版 800,以及
svn up
to get in sync with the repository.
与存储库同步。
I know the hash of the commit I want to get back to, so I tried
我知道我想返回的提交的哈希值,所以我尝试了
git reset <hash>
which seems to get me there. But then I tried
这似乎让我到了那里。但后来我试过了
git pull
but that complains about conflicts.
但这抱怨冲突。
So what's the proper way to move through the history of the branch?
那么在分支历史中移动的正确方法是什么?
I'm thinking in terms of SVN, so don't hezitate to point me to some nice tutorial. Note that I've already checked http://git.or.cz/course/svn.htmland http://www.youtube.com/watch?v=8dhZ9BXQgc4.
我正在考虑 SVN,所以不要犹豫给我指出一些不错的教程。请注意,我已经检查了http://git.or.cz/course/svn.html和http://www.youtube.com/watch?v=8dhZ9BXQgc4。
Thanks, Ondra.
谢谢,昂德拉。
采纳答案by Donny Kurnia
Well, I'm a former svn user too, and now use git for all my projects.
嗯,我以前也是 svn 用户,现在我所有的项目都使用 git。
When using git, you should change the way of thinking from the client-server architecture that's used in svn. In svn, every change needs a connection with server. Using git, your repo is in the working directory. You don't need a connection for every repo action.
使用git时,应该从svn中使用的客户端-服务器架构转变思维方式。在 svn 中,每次更改都需要与服务器连接。使用 git,您的 repo 位于工作目录中。您不需要为每个回购操作建立连接。
Only use git push
and git pull
to synchronise with repo. Think of it like using rsync or any backup solution, to make two place have exactly same content. Just like you connect external backup hard disk, then make the content in it same with the content in your main. That's the usage of git pull
and git push
.
仅使用git push
andgit pull
与 repo 同步。把它想象成使用 rsync 或任何备份解决方案,使两个地方具有完全相同的内容。就像你连接外部备份硬盘一样,然后让里面的内容和你的主硬盘中的内容一样。这就是git pull
and的用法git push
。
If you just want to go back and forth the history, do it using git checkout
. See the revision id using git history
. If you're using Linux, use gitk
to see the revision tree. In Windows, tortoise git can display it using revision graph.
如果您只想来回历史,请使用git checkout
. 使用git history
. 如果您使用的是 Linux,请使用gitk
来查看修订树。在 Windows 中,tortoise git 可以使用修订图来显示它。
To get back to latest revision, use git checkout master
. Before doing any command, always make yourself do git status
. This command will display anything you need to know about current repo condition, and what action that you need to do to make it right. Before do git pull
and git push
, it's better to make sure that git status
result is contain text working directory clean
.
要返回到最新版本,请使用git checkout master
. 在执行任何命令之前,总是让自己做git status
。此命令将显示您需要了解的有关当前 repo 条件的任何信息,以及您需要执行哪些操作才能使其正确。在执行git pull
and之前git push
,最好确保git status
结果包含 text working directory clean
。
If you need to revert a file to it's previous revision, you can do it with git merge
. Before doing it to a file, test it first with git diff
. Ex: git diff rev1:rev2 filename
. It will print out any different between two revision. Change in rev1 will be replaced by the changes in rev2. So to do revert, rev2 will be the older than rev1. After you satisfy with the diff result, do it with git merge
, just replace diff
with merge
, all other parameters stay the same.
如果您需要将文件恢复到以前的版本,您可以使用git merge
. 在对文件执行此操作之前,请先使用git diff
. 例如:git diff rev1:rev2 filename
。它将打印出两个修订版之间的任何差异。rev1 中的更改将替换为 rev2 中的更改。因此,要进行还原,rev2 将比 rev1 旧。当您与差异结果满足,这样做有git merge
,只需更换diff
与merge
所有其他参数保持不变。
I hope this helps you. The main key is to see that your working dir is your repo. Understanding this will help you use git to it's full capability. Good luck.
我希望这可以帮助你。主要的关键是看你的工作目录是你的仓库。理解这一点将帮助您充分利用 git。祝你好运。
回答by CB Bailey
You can use git checkout
to checkout any commit and then use it with a branch name to go back to a named branch.
您可以使用git checkout
检出任何提交,然后将其与分支名称一起使用以返回到命名分支。
git checkout
with a commit id and not a branch name moves you off any named branch and on to what is known as a detached head.
git checkout
使用提交 ID 而不是分支名称会将您从任何命名分支移到所谓的分离头上。
If you use git reset
then it will move your branch itself back to an old state, orphaning the more recent commits which probably isn't what you want.
如果您使用,git reset
那么它会将您的分支本身移回旧状态,孤立最近的提交,这可能不是您想要的。
回答by Raine Revere
The other answers are informative, but I believe this is closest to what the OP wants:
其他答案是有用的,但我相信这最接近 OP 想要的:
Add these two functions to your ~/.bashrc:
将这两个函数添加到您的 ~/.bashrc 中:
# checkout prev (older) revision
git_prev() {
git checkout HEAD~
}
# checkout next (newer) commit
git_next() {
BRANCH=`git show-ref | grep $(git show-ref -s -- HEAD) | sed 's|.*/\(.*\)||' | grep -v HEAD | sort | uniq`
HASH=`git rev-parse $BRANCH`
PREV=`git rev-list --topo-order HEAD..$HASH | tail -1`
git checkout $PREV
}
Usage:
用法:
$ git_prev
Previous HEAD position was 7042c8a... Commit message2
HEAD is now at d753ecc... Commit message1
$ git_next
Previous HEAD position was d753ecc... Commit message1
HEAD is now at 7042c8a... Commit message2
Note: These commands always enter detached HEADstate. If you git_prev
then git_next
from a currently checked out branch, you will end up back at the latest revision but you will be in detached HEAD state. Do git checkout BRANCH_NAME
to get back to normal.
注意:这些命令总是进入分离的 HEAD状态。如果您git_prev
然后git_next
从当前签出的分支中,您最终将返回到最新版本,但您将处于分离的 HEAD 状态。做git checkout BRANCH_NAME
才能恢复正常。
回答by Ryan Le
Try git reflog
, this lists commits and checkouts you have done to switch between the commits, even the commits you have lost when checkout to a previous commit.
Then you can try git checkout <hash of a commit>
to switch to that commit.
Try git reflog
,这会列出您在提交之间切换时所做的提交和检出,甚至是您在检出到上一个提交时丢失的提交。
然后你可以尝试git checkout <hash of a commit>
切换到那个提交。
Hope this helps!
希望这可以帮助!
回答by Jamey Hicks
To checkout a different version of a file, use
要签出文件的不同版本,请使用
git checkout rev -- filename
Where revcan be the ID of a commit, the name of a branch, the name of a tag, or a relative version.
其中rev可以是提交的 ID、分支的名称、标签的名称或相关版本。
Use git log
, gitk
to look examine versions to see which version of the file you want.
使用git log
,gitk
查看版本以查看所需文件的版本。
To make this version of the file permanent, you need to commit the file: git add filename; git commit filename
要使此版本的文件永久化,您需要提交该文件: git add filename; git commit filename
I would not recommend git pull
to examine versions because it does a merge -- potentially modifying your current state.
我不建议git pull
检查版本,因为它会合并——可能会修改您的当前状态。
You do not need to use git reset
in this case, unless you git add
a file you decide not to commit.
git reset
在这种情况下您不需要使用,除非git add
您决定不提交一个文件。