Another git question

So, I'm working in a git repo on my primary issue when I discover a different issue, fix it, but want to save just that one change (in one file) in a separate commit/branch because the change is completely unrelated to what I'm working on.

What's the correct (set of) incantation(s) without copying that one file out, reverting the change, committing/stashing my current work, reverting the repo to the original checkout, creating branch, copying that file back in, committing and pushing, then stash pop?

There's got to be a better way?
 

Marco

Ars Scholae Palatinae
1,346
Pretty sure you could skip at least some of those steps: just stage and commit everything but the change you would like to save in a different branch. Then you should, with the one change still unstaged, be able create the new branch right away (git checkout -b new-branch master assuming you want to base it off master), stage and commit, push it, and switch back to whatever you were working on. Then there's no need to copy/revert/stash/copy.
 

Lt_Storm

Ars Tribunus Angusticlavius
21,343
Subscriptor++
I was going to say: create a commit with everything else, check out the new branch, commit the one file to it, check out the branch you were working on, and git reset HEAD^ to undo the commit.

That said, stash has a --keep-index option, so it might be better to add the one file you want to commit to the other branch, git stash save --keep-index, check out the other branch, commit, check out the first branch and git stash pop.

Edit: also, don't forget, both stash save and add have --patch options. So, you can create partial commits with even bits of a file using them.
 

Lt_Storm

Ars Tribunus Angusticlavius
21,343
Subscriptor++
All this said, I can think of one potential problem: if the file you want to add a change to is different in the two branches, Git will complain when you try to check out the other branch, so, here is an option that is pretty much guaranteed to work even in that situation:

Code:
git checkout -b temp
git add <file I want to commit in other branch, maybe using --patch>
git commit -m <actual commit message I want in the other branch>
git stash save
git checkout <other-branch>
git cherry-pick temp
<maybe resolve merge conflicts>
git checkout <branch-I-was-working-on>
git stash pop
git branch -D temp


Though, rereading your note, it's possible the simplest answer is:

Code:
git add <file I want to commit separately, maybe with --patch>
git commit
git add --all
git commit

Though that creates two commits in the same branch. If that's a problem, then add a git checkout -b <new-branch-name> first, and checkout the other branch between.
 

Apteris

Ars Tribunus Angusticlavius
9,589
Subscriptor
Pretty you could skip at least some of those steps: just stage and commit everything but the change you would like to save in a different branch. Then you should just, with the one change still unstaged, be able create the new branch right away (git checkout -b new-branch master assuming you want to base it off master), stage and commit, push it, and switch back to whatever you were working on. Then there's no need to copy/revert/stash/copy.
Yes, this. Do this. If you run into trouble or your use case turns out to be more complicated, post here.

zelmak":1b6lbpis said:
What's the correct (set of) incantation(s) without copying that one file out, reverting the change, committing/stashing my current work, reverting the repo to the original checkout, creating branch, copying that file back in, committing and pushing, then stash pop?

There's got to be a better way?
There's no need to copy the file to a location outside the git repo, but the rest is stuff you need to do; it is essential complexity, not accidental.

You're on topic branch my-cool-feature. You've made 12 commits so far, and you have some staged changes, and some unstaged changes as well. Life is good.

Now you find "a different issue" which you want to fix. Fine. To which branch should the fix be applied? Let's say "master". So you need to navigate away from your location in the DAG, to master, and apply the fix there. Depending on your branching workflow, you might do anything from
Code:
git reset
git stash
git checkout master
# write code
git add -A
git commit -m "Different issue fix."
git push
git checkout -
git stash pop
to creating a new bug ticket in your issue tracker and taking it through the entire flow (which would involve most of the above steps, plus some others).

Git is very flexible as to how you navigate from your current location to the new one. You could keep your changes in place, assuming checking out a different branch won't overwrite anything, or you could commit them, or you could stash them. You could apply the fix either to a new branch created from "master", or to the current branch, to be cherry-picked later. It's all in your hands. This flexibility means there's several ways to achieve what you want, as Lt_Storm demonstrates above.

rain shadow":1b6lbpis said:
instead of dealing with swapping things in and out I always clone the project and/or a branch thereof to another directory and do stuff there instead of messing up my work in progress.
If I may, consider taking another look at the Git documentation or at a tutorial. It's almost impossible to mess up your work in progress, assuming even an average level of comfort with Git. I had to learn it -- our monolith is several gigabytes in size and has a complicated structure, so using multiple copies on my disk was a "LOL no" -- but I'm glad I did. It's a powerful tool.
 

rain shadow

Ars Tribunus Angusticlavius
6,679
Subscriptor++
rain shadow":7rih1qy1 said:
instead of dealing with swapping things in and out I always clone the project and/or a branch thereof to another directory and do stuff there instead of messing up my work in progress.
If I may, consider taking another look at the Git documentation or at a tutorial. It's almost impossible to mess up your work in progress, assuming even an average level of comfort with Git. I had to learn it -- our monolith is several gigabytes in size and has a complicated structure, so using multiple copies on my disk was a "LOL no" -- but I'm glad I did. It's a powerful tool.
I do like my separate copies though. It fits a unit test/regression test workflow where I frequently test branches against each other and verify that I didn't mess up anything. Obviously git recovers the state of the code if I do mess something up, but it doesn't restore context to my brain so there is still a lot of value to me with this workflow pattern.

Although I strongly suspect that if I learned git as well as I have learned other things I could do a career move and make an extra 50K without much effort. Mostly I'm glad that I'll never have to see cvs or vss again.
 

Lt_Storm

Ars Tribunus Angusticlavius
21,343
Subscriptor++
rain shadow":16sh14ps said:
instead of dealing with swapping things in and out I always clone the project and/or a branch thereof to another directory and do stuff there instead of messing up my work in progress.
If I may, consider taking another look at the Git documentation or at a tutorial. It's almost impossible to mess up your work in progress, assuming even an average level of comfort with Git. I had to learn it -- our monolith is several gigabytes in size and has a complicated structure, so using multiple copies on my disk was a "LOL no" -- but I'm glad I did. It's a powerful tool.
I do like my separate copies though. It fits a unit test/regression test workflow where I frequently test branches against each other and verify that I didn't mess up anything. Obviously git recovers the state of the code if I do mess something up, but it doesn't restore context to my brain so there is still a lot of value to me with this workflow pattern.

Although I strongly suspect that if I learned git as well as I have learned other things I could do a career move and make an extra 50K without much effort. Mostly I'm glad that I'll never have to see cvs or vss again.

This is not very different than multiple branches with git checkout other-branch replacing cd ../other-branch.... (Honestly, I find this preferable as I only have to point my editor at one directory.)

Also, you might consider looking into git worktree...
 

MilleniX

Ars Tribunus Angusticlavius
8,138
Subscriptor++
Zelmak, iirc, your background is something like physics or engineering rather than math or CS, but nevertheless, I think the articles The Git Parable and Git for Computer Scientists may be helpful in establishing a clear mental model for what Git is doing.

In my opinion (as the Git power-user of ever team I've been on and on Stack Overflow) Git is an incredible tool, but it absolutely demands that users actually understand what it's doing in order to to use it correctly, let alone even moderately well. I have seen this be a barrier to entry and a burden to collaboration over and over again. When very technically literate, hacker-minded CS professors have to ask for a cheatsheet of "which 5 incantations should I use", there's a big UX problem. Git is getting better about some of this with the most recent releases [1], but it's got a deep hole to climb out of.

[1] Particularly, moving away from commands like checkout and reset having multiple, wildly disparate use cases, and instead providing single-use commands for the majority of them. It's getting away from insisting on having a hand tool with a single flat blade serve as all of screwdriver, chisel, punch, can-opener, and scraper.
 
No, I'm a CS guy, and I understand various kinds of trees for the most part (I still struggle and have to lookup red-black tree implementations), but I come from CVS/SVN background and I can't seem to break that mental model. And attempting to follow that mental model when using git I find myself backed into corners. :)

I find that while git *is* very powerful, its kind of an arcane art dealing with its internals. Should I have to know how an internal combustion engine works to be effective driving a car -- yes, it CAN help, but isn't needed -- I keep finding the complete opposite whenever dealing with git, especially edge cases.

I've tried online courses/tutorials (and I'll check out the ones you referenced; I seem to remember going thru the latter), but they only do the easy stuff. Oops, I need to revert this; oh, I need to create a branch, etc., and not the random things I keep running into (probably because of my mental model?)

I'm trying, really, I am, but having to mentally deal with tree structures in my head meanwhile I'm trying to keep in my head those 19 things I'm actually developing, its a stretch.

Zelmak, iirc, your background is something like physics or engineering rather than math or CS, but nevertheless, I think the articles The Git Parable and Git for Computer Scientists may be helpful in establishing a clear mental model for what Git is doing.

In my opinion (as the Git power-user of ever team I've been on and on Stack Overflow) Git is an incredible tool, but it absolutely demands that users actually understand what it's doing in order to to use it correctly, let alone even moderately well. I have seen this be a barrier to entry and a burden to collaboration over and over again. When very technically literate, hacker-minded CS professors have to ask for a cheatsheet of "which 5 incantations should I use", there's a big UX problem. Git is getting better about some of this with the most recent releases [1], but it's got a deep hole to climb out of.

For the most part, we use TortoiseGit for our Windows workstations against our GitLab instance on our air-gapped, private network. But, it is just a thin veneer over Git; you still need to know what the commands do, which option flags do what, and such.

[1] Particularly, moving away from commands like checkout and reset having multiple, wildly disparate use cases, and instead providing single-use commands for the majority of them. It's getting away from insisting on having a hand tool with a single flat blade serve as all of screwdriver, chisel, punch, can-opener, and scraper.

And no, I don't mistake git's checkout for svn's checkout commands... I realized that a long time ago :)
 

MilleniX

Ars Tribunus Angusticlavius
8,138
Subscriptor++
So, the first thing I'll note is that you need to shift from thinking about tree structures to more general directed acyclic graphs (DAGs). You can't have merges in a tree :)

But yeah, it's the opposite of the extent of development of modern cars. You really do need to understand the operating principles of Git's engine in order to not blow it up with unfortunate regularity.