Git stash意外分支切换

时间:2017-03-09 06:32:48

标签: git

git stashgit stash pop是解决此问题的好方法吗?

我正在分支B上工作,但是我意外地发生了一些事情并且我不知道,把我带回了一个较旧的分支,分支A,我继续处理各种任务。

在我切换到分支B之前,Git希望我将我的新工作交给分支A,但我不能(不应该)这样做。

是否安全(意味着我不会失去我的所有工作,但能够将其置于正确的分支中),而在分支A(错误的分支)上,执行git stash,然后切换到分支B(正确的分支)并做git stash pop?这样做我会遇到任何灾难吗?

我不确定如何不要炸掉我的Git项目!

3 个答案:

答案 0 :(得分:3)

这似乎是一个恰当的举动。使用git stash将在您发现差异时保留您的工作索引,并允许您安全地将代码移动到您需要的分支。

正如一个FYI,git stash pop会将事物从存储中移出并进入索引,但也会删除存储中的内容。在我不是100%确定的情况下,我的偏好是使用git stash apply,所以我总是可以检索上一个隐藏的工作索引,直到我觉得足够安全删除它。

答案 1 :(得分:0)

我认为### SETUP ### import pygame, random pygame.init() c=pygame.time.Clock() window = pygame.display.set_mode([500, 400]) window.fill((255, 255, 255)) playing=True drop=1 x=0 points=0 highscore=0 dead=False height=random.randint(50,280) t=0 point_counter=1 play_again=False #start print("Press any key to start.") while not play_again: for event in pygame.event.get(): if event.type==pygame.KEYDOWN: play_again=True ### MAIN LOOP ### while playing: play_again=False for event in pygame.event.get(): if event.type == pygame.QUIT: playing = False ### GAME LOOP ### while playing and not dead: for event in pygame.event.get(): if event.type == pygame.QUIT: playing = False #jump if event.type==pygame.KEYDOWN: drop=-10 top_pipe=pygame.Rect(500+t,0,75,height) bird=pygame.Rect(200,200+x,75,50) window.fill((255,255,255)) #hitting ground or ceiling if bird.y>=350 or bird.y<=0: dead=True if top_pipe.x==-75: top_pipe.x=500 t=0 point_counter=1 height=random.randint(50,280) bottom_pipe=pygame.Rect(500-t,height-70,75,1000) t-=1 # if hits pipe if top_pipe.x<250 and top_pipe.x>150 and (bird.x<height or bird.x>height+20): print("You died!") dead=True elif top_pipe.x<150 and point_counter==1: points+=1 point_counter=0 #gravity drop+=1 x+=drop #body pygame.draw.rect(window,(255,255,0),bird) #eye pygame.draw.circle(window,(0,0,0),(bird.x+60,bird.y+10),2) pygame.display.flip() #pipes pygame.draw.rect(window,(0,255,0),top_pipe) pygame.draw.rect(window,(0,255,0),bottom_pipe) #framerate c.tick(30) x=0 drop=1 window.fill((255,255,255)) bird=pygame.Rect(200,200+x,75,50) #body pygame.draw.rect(window,(255,255,0),bird) #eye pygame.draw.circle(window,(0,0,0),(bird.x+60,bird.y+10),2) pygame.display.flip() if points>highscore: print("New highscore!: "+str(points)) highscore=points elif points==highscore: print("You matched your highscore!") else: print("Try again!") pygame.time.wait(2000) print("\n"*100) print("Highscore: "+str(highscore)) print("Press any key to play again.") dead=False while playing and not play_again: for event in pygame.event.get(): if event.type==pygame.QUIT: playing=False elif event.type==pygame.KEYDOWN: play_again=True 非常适合您的目的,据我所知,this帖子证实了我的观点。

也许你可能会遇到一些冲突需要解决。

我希望这会对你有所帮助。

答案 2 :(得分:0)

您当前的情况听起来是使用git stash的一个很好的理由。你说了以下几点:

  在我切换到分支B之前,Git希望我将我的新工作交给分支A,但我不能(不应该)这样做。

信不信由你,git stash实际上做了两个提交(有时甚至是三个)来保留你的工作目录和阶段,然后重置它们,允许你切换分支。

但是,您实际上可以自己进行临时提交。然后切换到分支B,你在那里工作,当你返回分支A时,你可以继续。当提交你的工作时,你实际上可以通过以下方式修改临时提交。

git stash

这将重写分支A上的HEAD提交,并且实际上没有你在那里做的临时未完成工作的痕迹。

相关问题