简体   繁体   English

如何从特定的提交继续项目并修复HEAD分离问题?

[英]How to continue project from specific commit and fix HEAD detached issue?

I checkouted specific commit from my project and continued from there, hoping that changes after that commit would be deleted, and that commit I checkouted would be new head. 我从项目中签出了特定的提交,然后从那里继续,希望该提交之后的更改将被删除,而我签出的提交将是新负责人。 I commited new changes, but I can't push them. 我提交了新的更改,但是我不能推动它们。 I'm still new to git. 我还是git的新手。

What I have done is: 我所做的是:

  1. git checkout commit_hash git checkout commit_hash
  2. edited project 编辑项目
  3. git commit -m "new changes" git commit -m“新更改”
  4. git push -u origin master git push -u原始主机

I got: 我有:

To https://github.com/myusername/project.git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'https://github.com/miloradsimic/ISA16.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

When I typed git status I got: 当我输入git status时,我得到:

HEAD detached from 506f0ec
nothing to commit, working directory clean

I changed project hierarchy, in head commit, so I want to get it back on previous state. 我在头提交中更改了项目层次结构,所以我想让它恢复到以前的状态。 I dont want to merge it with head. 我不想与头部合并。

I did it this way (easy way, not professional): I had in one folder project with changes, not positioned on head. 我是这样做的(简单的方法,不是专业的):我在一个文件夹项目中进行了更改,而不是放在头上。 I downloaded head commit into new folder, and copied all files (except git files) from folder with my changes to this folder. 我将head commit下载到新文件夹中,并将更改的内容从该文件​​夹复制了所有文件(git文件除外)。 Removed unnecessary files. 删除了不必要的文件。 Added all changes, commited and pushed. 添加了所有更改,已提交并已推送。

Thanks, Milorad Simic 谢谢,Milorad Simic

When we checkout to a commit-hash we are no longer on a branch ( detached HEAD ). 当我们签出提交哈希时,我们不再位于分支detached HEAD )上。

HEAD detached from 506f0ec HEAD与506f0ec分离

To solve it just create a new branch, do changes, do new Commits, Push to remote. 要解决此问题,只需创建一个新分支,进行更改,进行新的提交,推送到远程。

Checkout to a commit-hash. 签出到提交哈希。

$ git checkout <commit-hash>

You can do changes/commits here and create a new branch later before push to remote (your case here). 您可以在此处进行更改/提交并稍后创建新分支,然后再推送到远程(此处为您的案例)。 But I prefer to create a new branch (say, dev ) first. 但是我更喜欢先创建一个新分支(例如dev )。

$ git checkout -b dev
// do changes here

$ git add .
$ git commit -m 'message'
$ git push -u origin HEAD       # push to remote/dev

Merge dev branch into master branch: dev分支合并到master分支:

You need to merge dev branch into master branch to get your new changes. 您需要merge dev branchmaster分支以获取新更改。

$ git checkout master
$ git pull origin dev      # pull 'dev' new changes into 'master', pull = fetch + merge

$ git push origin HEAD     # update origin/master

Replace master branch with dev branch dev分支替换master分支

If you want to reset master with the changes of dev branch then replace master with dev branch. 如果你想重置master与变化dev分支然后更换masterdev分支。

$ git checkout dev
$ git branch -D master      # delete local master branch

$ git checkout -b master    # create new local/master branch with exactly 'dev's' history
$ git push -f origin HEAD   # force(-f) push to remote since git history is changed

Note: now master and dev have the same commits/changes. 注意:现在masterdev具有相同的提交/更改。

Check "git status", if you have changes remaining for commit first stash them for a while by "git stash". 检查“ git status”,如果您还有剩余更改要提交,请先通过“ git stash”将它们保存一段时间。

Try with "git pull --rebase" 尝试“ git pull --rebase”

After that try to commit again. 之后,尝试再次提交。 Let me know if you got same problem again. 让我知道您是否再次遇到相同的问题。

You can do git reset --hard <commit-id> 您可以执行git reset --hard <commit-id>

Then make your changes/commit. 然后进行更改/提交。

Cheers 干杯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM