1

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.

Alohomora
  • 417
  • 1
  • 5
  • 12
  • 1
    Please give a reason when downvoting a question, I don't think it's good to downvote without any reason, if there is any scope to explain my question better please let me know. – Alohomora Jun 29 '18 at 09:24
  • Do you have git installed locally? And did you work on the branch locally (or at a pc of a differend coworker?) – Ferrybig Jun 29 '18 at 09:34
  • 1
    where did you get the hash you are using? Is it same repository where you are running the command? How exactly you get it and what you did between getting it and running the command? – max630 Jun 29 '18 at 09:35
  • There are 3 states you can get from asking git this particular question. If it lists branches, then it list branches that contains the commit. Any branches you have that are not listed does not contain that commit. That's 2 states, a branch contains the commit, another branch doesn't. However, if you ask git about a commit that isn't even in the repository, you get that error message. That's the 3rd state. – Lasse V. Karlsen Jun 29 '18 at 10:22
  • 1
    If the commit is known by the remote, you might not have pulled or fetched it into your local repository. – Lasse V. Karlsen Jun 29 '18 at 10:23
  • If this is about Gerrit, why is it tagged as GitHub? – Oliver Charlesworth Jun 29 '18 at 10:29
  • @OliverCharlesworth Corrected, Thanks for that – Alohomora Jun 29 '18 at 13:36
  • Downvoting without a reason is very common. Just have to let it roll off our backs, it seems. – Don Branson Jun 29 '18 at 13:37
  • Try `git fetch` first then do your `git branch` command again. – Code-Apprentice Jun 29 '18 at 13:37
  • @Ferrybig, I've changed some part of the question could you please see it again – Alohomora Jun 29 '18 at 13:58
  • @Code-Apprentice, Tried, again the same error. – Alohomora Jun 29 '18 at 14:01
  • @LasseVågsætherKarlsen Tried that also, but it seems, it didn't work, could you please see the question again as I've added more description. – Alohomora Jun 29 '18 at 14:02
  • 1
    You must define better what you really did. When you say "I commited in Gerrit" you mean you have commited locally, pushed do Gerrit and finally submitted (merged) in Gerrit, right? When you say "I deleted the branch" you mean you have deleted the local branch, correct? Or you have deleted the remote (Gerrit) branch? – Marcelo Ávila de Oliveira Jun 29 '18 at 14:04
  • @MarceloÁviladeOliveira Thanks, I've edited. When I said " I committed in Gerrit" I mean to say pushing to Gerrit and merged in Gerrit, and When I said " I deleted the branch" I mean to say that I deleted the extra branch I created locally. – Alohomora Jun 29 '18 at 14:09

4 Answers4

0

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:

Will
  • 6,601
  • 3
  • 31
  • 42
  • Sorry but I got error: no such commit 31436fd7200566967f85bfb1ee5425f9b599b908 – Alohomora Jun 29 '18 at 14:47
  • That indicates that you did not push it to the remote. – Will Jun 29 '18 at 14:59
  • Ah, thanks, I see. Hmm, so the issue must be specific to gerrit. – Will Jul 09 '18 at 15:48
  • Posted a different answer based on this information. – Will Jul 09 '18 at 15:57
  • You are seeing the `no such commit` error because the project is configured to use the `Cherry Pick` submit strategy, and as such a new commit was created when it was submitted. You can see the new commit's sha1 in the message: `Change has been successfully cherry-picked as 2d61cb923e5e3999f6fd660aec26504311f0407c` – David Pursehouse Jul 11 '18 at 05:45
0

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
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.

Will
  • 6,601
  • 3
  • 31
  • 42
0

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.

David Pursehouse
  • 933
  • 6
  • 14