简体   繁体   中英

Getting “error: no such commit” when trying to restore a deleted branch?

I have already committed a file in Gerrit(pushed it), but I think, I, unfortunately, deleted that branch locally from which I committed. How to fix that? Please help.

I tried

    git branch --contains 31436fd7200566967f85bfb1ee5425f9b599b908

but it shows

    error: no such commit 31436fd7200566967f85bfb1ee5425f9b599b908

Edit: Yes I've git installed locally and I worked on the branch locally.

I got commit id from the Gerrit site, I've committed.

Okay, two days ago first there was a problem when I tried to commit the change, say A. I tried to submit it using "./logerrit submit master" but all I got was:

    Counting objects: 32, done.
    Delta compression using up to 4 threads.
    Compressing objects: 100% (32/32), done.
    Writing objects: 100% (32/32), 100.46 KiB | 0 bytes/s, done.
    Total 32 (delta 22), reused 0 (delta 0)
    remote: Resolving deltas: 100% (22/22)
    remote: Counting objects: 79789, done
    remote: Processing changes: refs: 1, done    
    To ssh://logerrit/core
    ! [remote rejected] HEAD -> refs/for/master (change 
    https://gerrit.libreoffice.org/54112 closed)
    error: failed to push some refs to 'ssh://logerrit/core'

So, it says that change id is same for two commits- the patch I'm committing and the patch that was already committed by me a few days ago and is merged now. I tried to do every possible thing which is mentioned on stack overflow or any other blogs- I tried to change the id, such that on next "git commit --amend" git automatically change the id, it was of no use as I was getting the same error, I also tried to fix the conflicts by removing the lines <<<<<<<<<<< ============ and >>>>>>>>>>> but am I correct with it? Is it the right way to resolve it? I then tried to "git rebase master" and then I got the error message:

    First, rewinding head to replay your work on top of it...
    Applying: smartart : test documents
    Using index info to reconstruct a base tree...
    M   sd/qa/unit/import-tests-smartart.cxx
    Falling back to patching base and 3-way merge...
    Auto-merging sd/qa/unit/import-tests-smartart.cxx
    CONFLICT (content): Merge conflict in sd/qa/unit/import-tests- 
    smartart.cxx
    error: Failed to merge in the changes.
    Patch failed at 0001 smartart : test documents
    The copy of the patch that failed is found in: .git/rebase- 
    apply/patch

    When you have resolved this problem, run "git rebase --continue".
    If you prefer to skip this patch, run "git rebase --skip" instead.
    To check out the original branch and stop rebasing, run "git rebase --abort".

I tried to change the branch, and then tried to again rebase the changes with the master as it was already up-to-date, but It was of no use. I then deleted the extra branches "locally" I created, and by mistake, I might have deleted one of the branch from which I've committed to Gerrit.

Now my question is:

  1. Can I get my deleted branch restored again in my local system? Is it possible?
  2. How to resolve merge issues or the error you saw after rebasing. I have a strong doubt, I might be doing wrong something. Please help and please let me know if I didn't cover anything.

Both first and second questions are related to differnt files/branch, but could be related to each other.

Since you are certain the commit exists on the remote, this should find it:

git branch -r --contains 31436fd7200566967f85bfb1ee5425f9b599b908

Once you know which remote branch the commit you want is on, let's call it <branch> , then you should be able to recreate that same branch locally with:

# delete same-named local branch (if it exists)
git branch -D <branch>
# fetch latest branches from remote
git fetch
# checkout branch from remote
git checkout <branch>

Aknowledgements:

Forget the commit 31436fd7200566967f85bfb1ee5425f9b599b908. If you really have merged your commit in Gerrit it is on the remote branch. To create and checkout a local branch from a remote branch execute:

git checkout -b local-branch remotebranch

Ex:

git checkout -b my-feature origin/release1.0

I don't know if this will meet your needs, but you should be able to checkout the current master branch freshly from the remote and then apply the same change you are trying to obtain. Unless you have some additional changes you haven't mentioned, then this should give you everything you need to replay the change and move on.

These steps will destructively delete your local master and restore it from the remote:

# delete local master
git branch -D master
# fetch latest branches from remote
git fetch
# checkout master fresh from remote
git checkout master

Then manually copy the contents from the gitweb resource:

https://gerrit.libreoffice.org/gitweb?p=core.git;a=commitdiff;h=31436fd7200566967f85bfb1ee5425f9b599b908

...into the only file that changed in that commit, sd/qa/unit/import-tests-smartart.cxx .

The result is a current master branch with the change you seek applied. Next I assume you would either create a new branch with git checkout -b <new-branch> or stay in master, then commit the change. Finally you can push as appropriate.

When using Gerrit, it is possible that the commit that ends up in the repository has a different sha1 than the original commit that you pushed. This can happen for several reasons:

  • The change gets rebased by a user (either in the UI, or by someone downloading it and pushing a rebased patch set), or a new patch set is uploaded
  • The change gets rebased on submit, if the project is using the Rebase if Necessary submit strategy
  • The project is using the Cherry Pick strategy, and a new commit gets created for all changes on submit.

You can see the actual commit sha1 in the Gerrit UI. The exact location of that depends on which version of Gerrit, and which UI (GWT or Polymer) is used.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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