.git: fetch and apply commits of removed branch
You can manually download missing
commits in mbox
format and apply them manually with git am
. For
example:
github
(repository you linked to):$ wget https://github.com/yurybond/stackowerflow-rocks/commit/a1c1540abd453773b3ce6445d01e51ad336bbe84.patch && git am a1c1540abd453773b3ce6445d01e51ad336bbe84.patch
gitlab
:$ wget https://git.weboob.org/weboob/devel/commit/bba7e1b8ffb0743b57f202cf9cdb43fda209fa43.patch && git am bba7e1b8ffb0743b57f202cf9cdb43fda209fa43.patch
bitbucket
:$ wget https://bitbucket.org/Kasreyn/linux-3-9-rc3-moxart/commits/434e8f69db2c3effdc8741139adb722a68dfcccd/raw -O 434e8f69db2c3effdc8741139adb722a68dfcccd.patch && git am 434e8f69db2c3effdc8741139adb722a68dfcccd.patch
Notice that git am
will not only apply textual patch but will also
recreate a whole commit on the current branch.
If you removed a branch from the remote repository there is no way to
get it back from the remote reposityr. However, normally you still
might be able to restore a removed branch locally thanks to git reflog
. See the following
example.
First, create a new non-bare repository:
$ git init
Initialized empty Git repository in /tmp/reflog-test/.git/
Add file
and create a new commit on master
branch:
$ touch file
$ git add .
$ git commit -m 'Initial commit'
[master (root-commit) 81fc76d] Initial commit
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 file
Switch to a new branch creatively named new-branch
:
$ git checkout -b new-branch
Switched to a new branch 'new-branch'
Modify file
and commit changes:
$ echo new-branch >> file
$ git commit -am 'commit on new-branch'
[new-branch 9c457c6] commit on new-branch
1 file changed, 1 insertion(+)
Switch back to master
:
$ git checkout -
Switched to branch 'master'
$ git branch
* master
new-branch
Remove new-branch
$ git branch -D new-branch
Deleted branch new-branch (was 9c457c6).
It's gone:
$ git branch
* master
$ git log new-branch
fatal: ambiguous argument 'new-branch': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'
However, you should still be able to reference the commit:
$ git show --stat 9c457c6
commit 9c457c69de8a54376a2614ca8cfc0f515c64676c
Author: Arkadiusz Drabczyk <[email protected]>
Date: Tue Apr 3 10:43:49 2018 +0200
commit on new-branch
file | 1 +
1 file changed, 1 insertion(+)
Even though it can't be found on any branch. The following command returns nothing:
$ git branch --contains 9c457c6
Reflog
shows all actions one did in a repository:
$ git reflog
dbc721a HEAD@{0}: checkout: moving from new-branch to master
9c457c6 HEAD@{1}: commit: commit on new-branch
dbc721a HEAD@{2}: checkout: moving from master to new-branch
dbc721a HEAD@{3}: commit (initial): Initial commit
As you see it also has 9c457c6
commit we did on the new-branch
.
Only when reflog
is expired and garbage collector is run 9c457c6
becomes unreachable:
$ git reflog expire --expire=all --all
$ git gc --prune=now
Counting objects: 3, done.
Writing objects: 100% (3/3), done.
Total 3 (delta 0), reused 0 (delta 0)
$ git show 9c457c6
fatal: ambiguous argument '9c457c6': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'
By default reflog
entries expire after 90 days and git gc
is run
automatically after some commands so if you have removed the branch
relatively recently you should be able to restore it.
If this commit does actually exist in the Bitbucket repository, then you can merge it into another branch using the Bitbucket web interface.
- Go to branches --> Create a new branch in Bitbucket. Name it whatever you want
- When the branch is created, click compare (the three dots menu in top right)
- In the compare screen, select change source and paste in your commit reference. If it comes up with a result, then you're in luck. Select it.
- Select change destination, and change it to your new branch.
- You should be able to browse the difference, seeing all the changes you want to bring in.
- Select merge if you're happy.
Now here's the kicker. If pasting that commit reference at step 3 fails, then you're out of luck. It means the commit does not exist either on the remote or in your local.
If you hate navigating and want a direct link to the compare (will compare to master, though): https://bitbucket.org/<user>/<repository>/branches/compare/<commit-reference>..master