Git checkout 提交,做一些事情,然后回到 master
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12043491/
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 checkout commit, do stuff, and go back to master
提问by Ronze
I have a build system that takes a git repository, goes back to a specific commit, and then uploads those files some where. Then is goes back to master.
我有一个构建系统,它需要一个 git 存储库,返回到特定的提交,然后将这些文件上传到某个地方。然后是回到主人。
I'm unsure that I'm using the right Git commands, as Git will give me this message whenever I perform a git checkout SHA
:
我不确定我使用的是正确的 Git 命令,因为每当我执行以下操作时,Git 都会给我这条消息git checkout SHA
:
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
The only thing I want to do is to reset my working directory to a specific commit, upload those files, and go back to HEAD/master. I might make a few changes to files when I go to the specific commit SHA (convert XML to JSON or something), but I just want to loose all of those changes when I go back to master. Right now this is my code
我唯一想做的就是将我的工作目录重置为特定的提交,上传这些文件,然后返回 HEAD/master。当我转到特定的提交 SHA(将 XML 转换为 JSON 或其他内容)时,我可能会对文件进行一些更改,但是当我回到 master 时,我只想取消所有这些更改。现在这是我的代码
git checkout SHA
# do a bunch of conversion and uploading
git checkout master
Is that the preferred way of doing what I want? Will I always be able to do pulls from the origin without getting any file conflicts (I don't want to keep whatever I'm doing between the checkouts)?
这是做我想做的事情的首选方式吗?我是否总是能够在不发生任何文件冲突的情况下从原点进行拉取(我不想在结帐之间保留我正在做的任何事情)?
I'm asking because I'm seeing "your master and origin/master diverged" some times, although I'm not sure it's caused by this.
我问是因为我有时看到“你的主人和起源/主人分歧”,尽管我不确定这是由这个引起的。
Thanks in advance.
提前致谢。
采纳答案by Penghe Geng
It's perfectly OK to do this. When HEAD isn't corresponding to a branch name it will be shown as a detached HEAD. Nothing wrong with that.
这样做完全没问题。当 HEAD 与分支名称不对应时,它将显示为分离的 HEAD。没有错。
回答by lisachenko
You can also use git show SHA1:relative/path
to take a snapshot of file at specific point.
您还可以使用git show SHA1:relative/path
在特定点拍摄文件快照。
回答by vonbrand
For what you describe, you can just do something like:
对于您所描述的内容,您可以执行以下操作:
git archive -o /some/where/archive.tar.gz --prefix=<something> <commit> <file list>
git archive -o /some/where/archive.tar.gz --prefix=<something> <commit> <file list>
Look at the manual for git archive for the gory details. You can create zip files, tar files (compressed or not), opionally place a prefix "above" the packed files, all taken from the referenced commit.
查看 git archive 的手册以了解详细信息。您可以创建 zip 文件、tar 文件(压缩或未压缩),或者在打包文件的“上方”放置一个前缀,所有这些都取自引用的提交。
回答by Jasper Blues
This is what I use to publish API, test and code coverage reports back up to my github page (gh-pages branch) after a successful Bamboo build. Its an Ant target that does the kind of thing you describe. (Lots of good answers here, but posting in case it helps).
这是我在 Bamboo 构建成功后用于将 API、测试和代码覆盖率报告备份到我的 github 页面(gh-pages 分支)的内容。它是一个 Ant 目标,可以执行您所描述的那种事情。(这里有很多好的答案,但如果有帮助,请张贴)。
<target name="--publish.site">
<echo file="${temp.dir}/publish-site.sh">
#!/bin/sh
cd ${basedir}
cp -fr ${basedir}/schema ${reports.dir}
git remote set-url origin [email protected]:jasperblues/my-project.git
git fetch origin gh-pages:gh-pages
git checkout gh-pages
git pull
rm -fr ./coverage
cp -fr ${reports.dir}/coverage/ ./coverage
git add ./coverage
rm -fr ./api
cp -fr ${reports.dir}/api ./api
git add api
cp -fr ${reports.dir}/schema ./schema
git add schema
git commit -a -m "publish reports to gh-pages"
git push -u origin gh-pages
git checkout master
</echo>
<chmod perm="+x" file="${temp.dir}/publish-site.sh"/>
<exec executable="${temp.dir}/publish-site.sh" failonerror="true" failifexecutionfails="true">
<env key="PATH" value="${tools.paths}"/>
</exec>
</target>
回答by harpun
You mentioned pulling changes from the origin. In your case you may also use git reset --hard SHA1
to go back to a specific commit and then git pull
the missing changes from origin/master
.
您提到从原点提取更改。在您的情况下,您还可以使用git reset --hard SHA1
返回到特定提交,然后git pull
从origin/master
.