If I run git branch -d XYZ
, is there a way to recover the branch? Is there a way to go back as if I didn't run the delete branch command?
相关问题
- Why does recursive submodule update from github fa
- Extended message for commit via Visual Studio Code
- Emacs shell: save commit message
- Can I organize Git submodules in a flat hierarchy?
- Upload file > 25 MB on Github
相关文章
- 请教Git如何克隆本地库?
- GitHub:Enterprise post-receive hook
- Git Clone Fails: Server Certificate Verification F
- SSIS solution on GIT?
- Is there a version control system abstraction for
- ssh: Could not resolve hostname git: Name or servi
- Cannot commit changes with gitextensions
- git: retry if http request failed
A related issue: I came to this page after searching for "how to know what are deleted branches".
While deleting many old branches, felt I mistakenly deleted one of the newer branches, but didn't know the name to recover it.
To know what branches are deleted recently, do the below:
If you go to your Git URL, which will look something like this:
Then you can see the feed, of what is deleted, by whom, in the recent past.
Most of the time unreachable commits are in the reflog. So, the first thing to try is to look at the reflog using the command
git reflog
(which display the reflog forHEAD
).Perhaps something easier if the commit was part of a specific branch still existing is to use the command
git reflog name-of-my-branch
. It works also with a remote, for example if you forced push.If your commits are not in your reflog (perhaps because deleted by a 3rd party tool that don't write in the reflog), I successfully recovered a branch by reseting my branch to the sha of the commit found using a command like that (it creates a file with all the dangling commits):
If you should use it more than one time (or want to save it somewhere), you could also create an alias with that command...
and use it with
git rescue
To investigate found commits, you could display each commit using some commands to look into them.
To display the commit metadata (author, creation date and commit message):
To see also the diffs:
Once you found your commit, then create a branch on this commit with:
I used the following commands to find and retrieve my deleted branch. The first steps are from gcb's description.
Now look for the git commit id (GIT-SHA) based on the commit comments and use it in the command below. Checkout a new branch called NEW-BRANCH with the previously found GIT-SHA:
For recovering a deleted branch, First go through the reflog history,
Where n refers to the last n commits. Then find the proper head and create a branch with that head.
BIG YES
if you are using GIT follow these simple steps https://confluence.atlassian.com/bbkb/how-to-restore-a-deleted-branch-765757540.html
if you are using smartgit and already push that branch go to origin, find that branch and right click then checkout
If you like to use a GUI, you can perform the entire operation with gitk.
This will allow you to see the branch's commit history as if the branch hadn't been deleted. Now simply right click on the most recent commit to the branch and select the menu option
Create new branch
.