2015-04-01 143 views
1

我有两个提交,我已经提交和推送。 我需要返回代码以声明'两次提交前'。撤销更改(提交并推送)

我可以用git reset --hard <hash>在本地做,但我不能推这种变化:

提示:更新被拒绝,因为你的当前分支的尖端后面

哪有我将代码返回到中央存储库中的一些以前的提交?

更新:

ms-app-actual/mobile-application » git reset --hard e50fa38c4865bd82fce7ddcf1e05d94012266364 ‹master› 
HEAD is now at e50fa38 Move Attachment class to separate Project 
    ms-app-actual/mobile-application » git push --force           ‹master› 
Total 0 (delta 0), reused 0 (delta 0) 
remote: GitLab: You don't have permission 
To ssh://[email protected]:2222/ms-mobile-app/mobile-application.git 
! [remote rejected] master -> master (pre-receive hook declined) 
error: failed to push some refs to 'ssh://[email protected]:2222/ms-mobile-app/mobile-application.git' 

但我可以拉:

ms-app-actual/mobile-application » git pull             ‹master› 
Updating e50fa38..cebcabf 
Fast-forward 

,使普通的提交。

我正在使用Gitlab。

+2

'git push --force'怎么样? – raina77ow 2015-04-01 16:31:23

+0

@ raina77ow既然这是正确的答案,为什么不把它作为一个发布呢? – Powerlord 2015-04-01 16:33:31

+0

嗯...我已经更新了我的问题。 – demas 2015-04-01 16:35:12

回答

2

做一个git reset --hard并强制推送更改将删除提交。但是,您需要小心这样做。如果其他人已经撤回了您的更改,则会导致他们的历史记录出现问题。看来Gitlab无论如何都阻止你这样做。

最好的方法是创建一个新的提交,撤消更改并推送它。通过这种方式,任何人都不会在变更中合并任何问题。有几种方法可以做到这一点。

1)创建两个复归提交和他们重订成一个

git revert <sha of commit 1> 
git revert <sha of commit 2> 
git rebase -i <sha of commit is in good state> 
//Squash the two revert commits into one 
git push 

2)结帐所有修改过的文件从良提交和提交这些更改

git diff --name-only HEAD..HEAD~3 | xargs git checkout HEAD~3 -- 
//This should result in all the files modified in the two commits being changed. 
git commit -am "Reverting changes from commits" 
git push 

这些解决方案的两个,基本上导致同样的事情。一个新的提交与您在两个不需要的更改中相反。这是撤销推送到远程存储库的更改的最安全方法。

作为一条经验法则:一旦将更改推送到远程存储库,您应该认为它们是永久性的。

既然你也试图做reset --hard,为了获得提交,以便你可以撤消它们。所有你需要做的是git pull