Git:暂时还原推送提交(s) [英] Git: temporarily reverting pushed commit(s)

查看:165
本文介绍了Git:暂时还原推送提交(s)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,你已经推动了一些提交并将它们投入生产,例如在你的服务器的webroot中。然后出了点问题。很显然,你想要做的就是暂时将webroot中的文件恢复到之前的状态,然后返回到本地开发位置,修复损坏的内容,对其进行测试,在提交之后进行提交,然后推送新的修改向主分支提交。然后再次转到生产webroot并将所有内容都提交到最新的提交,因此所有内容都已修复并正常工作。然后,每个人都可以将主分支拉出来,而不用担心提交失败或重新设置git head或其他令人沮丧的事情。

所以:它是一种合法和安全的方法在生产webroot,master分支上

 > git log --pretty = format:%h%ad |%s [%an]--date = short 

0fu83bd Wed Mar 6 2013年17:47:42 |合并分支'sample'[developer1]
fd442f8 Wed Mar 6 17:47:10 2013 |一些更新[developer1]
ad84471 Wed Mar 6 17:25:12 2013 |添加一些内容[b] [b] [b] [b] [b] [b] [b] [b] [b] [b] [b] [b] [b]找到要暂时恢复文件状态的提交,说: ad84471

 > git checkout ad84471 
> git分支
*(no branch)
master

去你身边的任何地方开发,修复,提交,[合并],推主分支。当你这样做时,生产文件处于ad84471状态,没有人修改它们。然后回到生产webroot:

 > git checkout master 
> git pull
> git branch
* master
> git log --pretty = format:%h%ad |%s [%an]--date = short
7guffbd Wed 3月6日17:47:42 2013 |修复0fu83bd错误[developer1]< --- new commit
0fu83bd Wed Mar 6 17:47:42 2013 |合并分支'sample'[developer1]
fd442f8 Wed Mar 6 17:47:10 2013 |一些更新[developer1]
ad84471 Wed Mar 6 17:25:12 2013 |现在我们在主分支中,并且所有的东西都可以正常工作。[b] [b] [b] [b] [b] [b] [b] 。每个人只需要取得最新的变化并且很好。



我已经使用md5deep检查了这些文件,以确保所有内容都返回(在修复之前)我们已经恢复的状态:

 > md5deep -rel webroot> hashes_master_before_checkouting_ad84471 
> git checkout ad84471
> git checkout master
> md5deep -rel webroot> hashes_master_after_checkouting_master_again

这些哈希之间的差异仅显示

  webroot / .git / logs / HEAD 
webroot / .git / index

发生了变化。



因此,它似乎是一个很好的方法来快速修复或者我错了?



免责声明:我知道,在很多项目中,这违背了预期的工作流程,而且这种做法不太好,此外,应该有自动化测试,但对于少数开发人员的小项目来说,这往往是不可能或不实际的,所以与使用git reset或revert相比,此方法可以节省大量时间并简化工作。

解决方案

如果您想成为一名急救医生多一点精简,你可以改为从已知的好提交中创建一个分支,并检查生产中的分支,

在开发机器上:

  git branch hotfix-20130307 ad84471 
git push origin hotfix-20130307

关于prod machine:

  git fetch 
git checkout hotfix-20130307

然后在master分支修复之后,只需在prod机器上重新签出master并更新它即可:

  git checkout master 
git pull

这样做的好处是,如果有人前往prod机器并运行 git branch ,他们会得到更多信息,并且可能会找到修补程序的某些记录-20130307 在错误跟踪器中分支,并知道它为什么完成。如果这是一个小团队,并且唯一可能看到prod机器的人将已经知道情况是什么,那么这可能是矫枉过正。


Say, you've pushed some commits and pulled them into production, in your server's webroot, for example. And then something goes wrong. Clearly, most often what you want to do is temporarily revert the files in the webroot to some previous state, then go back to your local development place, fix what is broken, test it, commit on top of commits that broke something and push these new fixing commits to the master branch. And then just go to the production webroot again and pull everything to the latest commit so everything is fixed and works correctly. Everyone then can just pull the master branch and not worry about broken commits or resetting git head or other frustrating things.

So: is it a legitimate and safe method to do

In the production webroot, on master branch

>git log --pretty=format:"%h %ad | %s [%an]" --date=short

0fu83bd Wed Mar 6 17:47:42 2013 | Merge branch 'sample' [developer1]
fd442f8 Wed Mar 6 17:47:10 2013 | Some updates [developer1]
ad84471 Wed Mar 6 17:25:12 2013 | Added something [developer2]

find the commit you want to temporarily revert the files state to, say ad84471

>git checkout ad84471
>git branch
* (no branch)
  master

go to wherever you develop, fix, commit, [merge], push master branch. While you do so, the production files are in ad84471 state and no one modifies them. Then back in the production webroot:

>git checkout master
>git pull
>git branch
* master
>git log --pretty=format:"%h %ad | %s [%an]" --date=short
7guffbd Wed Mar 6 17:47:42 2013 | Fixed 0fu83bd bugs [developer1]   <---new commit
0fu83bd Wed Mar 6 17:47:42 2013 | Merge branch 'sample' [developer1]
fd442f8 Wed Mar 6 17:47:10 2013 | Some updates [developer1]
ad84471 Wed Mar 6 17:25:12 2013 | Added something [developer2]

now we are in the master branch, and everything works as should. everyone just pull the latest changes and are good to go.

I've checked the files with md5deep to ensure everything returns (before pulling the fix) to the state we've reverted from:

>md5deep -rel webroot > hashes_master_before_checkouting_ad84471
>git checkout ad84471
>git checkout master
>md5deep -rel webroot > hashes_master_after_checkouting_master_again

the diff between these hashes shows only

webroot/.git/logs/HEAD
webroot/.git/index

had changed.

So it seems like a good method to quickly fix something, or maybe I am wrong?

Disclaimer: I do know that in lots of projects this goes against the intended workflow, and that this practice is not so good, and also that there should be automated testing before, but for small project with few developers it is often not possible or practical, so this method could save a lot of time and simplify the things compared to using git reset or revert.

解决方案

Seems fine to me for an emergency fix.

If you wanted to be a bit more "rigourous" you could instead create a branch from the "known good" commit and check that out on production, as it leaves a bit more of an audit trail.

On the dev machine:

git branch hotfix-20130307 ad84471
git push origin hotfix-20130307

On the prod machine:

git fetch
git checkout hotfix-20130307

Then after the master branch is fixed, just checkout master again on the prod machine and update it:

git checkout master
git pull

The benefit of this is that if someone goes to the prod machine and runs git branch they get something more informative, and can presumably find some record of the hotfix-20130307 branch in a bug tracker, and know why it was done. If it's a small team and the only people likely to look at the prod machine will already know what the situation is then that might be overkill.

这篇关于Git:暂时还原推送提交(s)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆