jesseduffield / lazygit

simple terminal UI for git commands
MIT License
50.49k stars 1.78k forks source link

Offer to autostash changes when creating a new branch #3866

Closed brandondong closed 1 week ago

brandondong commented 2 weeks ago

Is your feature request related to a problem? Please describe. Sometimes, I'm on branch A and then I want to create and switch to a new branch based off of branch B. So I navigate to the B branch in the branches panel and press n. However, if I have changed files, this can present the git error where I must commit or stash my changes first. It's often the case that lazygit's autostash feature would have saved me but the functionality is not present in this flow.

I don't see a reason why not. The action is basically the same as checking out B in terms of moving the HEAD pointer and the autostash feature is present there. Whenever I run into this, I just press <space> instead which triggers the autostash push and pop, and then press n.

Describe the solution you'd like When creating a new branch and git returns the commit or stash error, offer to autostash.

Describe alternatives you've considered A clear and concise description of any alternative solutions or features you've considered.

Additional context Reproduction:

git init
echo -e "a\n\nb" > file
git add .
git commit -m "add file"
echo -e "a\n\nc" > file
git add .
git commit -m "edit last line"
git checkout -b dev HEAD~
echo -e "b\n\nb" > file
lazygit

Checking out the other branch (<space>) will work but trying to create and switch to a new branch based off of it (n) will not.

stefanhaller commented 2 weeks ago

Funny, I was thinking about this too the other day. I ran into this several times in the past, and it's always annoying. I was about to make a PR for this sometime soon, but I have little time, and I wouldn't mind if you beat me to it, @brandondong. Wanna give it a try?

I suppose it would be nice to create some abstraction that lets us share the code between checkout and new branch, but I haven't checked if that's feasible. I would also be ok with simply duplicating the code from checkout, using the same approach.

brandondong commented 2 weeks ago

Sure, can take a crack at it later.