Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
415 views
in Technique[技术] by (71.8m points)

git - 在Git中删除分支后能否恢复分支?(Can I recover a branch after its deletion in Git?)

If I run git branch -d XYZ , is there a way to recover the branch?

(如果我运行git branch -d XYZ ,有没有办法恢复分支?)

Is there a way to go back as if I didn't run the delete branch command?

(有没有办法像我没有运行delete branch命令一样返回?)

  ask by prosseek translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Yes, you should be able to do git reflog and find the SHA1 for the commit at the tip of your deleted branch, then just git checkout [sha] .

(是的,您应该能够执行git reflog并在已删除分支的尖端找到提交的SHA1,然后只需执行git checkout [sha] 。)

And once you're at that commit, you can just git checkout -b [branchname] to recreate the branch from there.

(一旦完成该提交,就可以git checkout -b [branchname]从那里重新创建分支。)


Credit to @Cascabel for this condensed/one-liner version.

(感谢此压缩/单线版本的@Cascabel。)

You can do it in one step:

(您可以一步完成:)

git checkout -b <branch> <sha>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...