Skip to contentSkip to conversation

Problems switching branches

DISCUSSION Maaartin 3 participants 6 messages Jun 15, 2016

lore
Share & tools
lore

Maaartin

author
#0 lore raw

I sometimes run in a problem similar to
http://kerneltrap.org/mailarchive/git/2008/10/15/3667644/thread
There are some ignored files which I want neither track nor throw away; I'm just happy to have them and keep them out of version control.

Unfortunately, there weren't ignored in the old branch. I'd be quite happy with non-destructive switching like "checkout everything what doesn't overwrite an untracked file", so I would end in the old branch with a dirty working tree. Is it possible?

author
#1 lore raw
Quoted 4 lines · Maaartin <grajcar1 <at> seznam.cz> writes:

I sometimes run in a problem similar to
http://kerneltrap.org/mailarchive/git/2008/10/15/3667644/thread
There are some ignored files which I want neither track nor throw away; I'm

just

Quoted 3 lines

happy to have them and keep them out of version control.

Unfortunately, there weren't ignored in the old branch. I'd be quite happy

with

Quoted 2 lines

non-destructive switching like "checkout everything what doesn't overwrite an untracked file", so I would end in the old branch with a dirty working tree.

Is

Quoted 1 line

it possible?

No answer? In the meantime I've found a simple workaround: Checkout the old branch in another directory, modify .gitignore, commit and retry. But this is far from cool. Am I doing something stupid and nobody else ran into this?

#2 lore raw
Quoted 9 lines · Maaartin
Quoted 4 lines · Maaartin <grajcar1 <at> seznam.cz> writes:

I sometimes run in a problem similar to
http://kerneltrap.org/mailarchive/git/2008/10/15/3667644/thread
There are some ignored files which I want neither track nor throw away; I'm

just

Quoted 2 lines

happy to have them and keep them out of version control.

Quoted 8 lines
Quoted 1 line

Unfortunately, there weren't ignored in the old branch. I'd be quite happy

with

Quoted 2 lines

non-destructive switching like "checkout everything what doesn't overwrite an untracked file", so I would end in the old branch with a dirty working tree.

Is

Quoted 1 line

it possible?

No answer?

Which means nobody is interested in. Well, not really.

I also get irritated by a similar situation, where the untracked files have the same content as the to-be-checked-out files. I have been tempted (but never got around) to make git compare the in-index content and the untracked file, if it's the same, no need to abort the checkout process.

But your approach may be better. Yes, I think it's possible. Any suggestion for checkout's new argument? --no-overwrite-untracked seems too long.

Signature · 1 line
Duy
author
#3 lore raw
Quoted 19 lines · Nguyen Thai Ngoc Duy
Quoted 9 lines · Maaartin
Quoted 4 lines · Maaartin <grajcar1 <at> seznam.cz> writes:

I sometimes run in a problem similar to
http://kerneltrap.org/mailarchive/git/2008/10/15/3667644/thread
There are some ignored files which I want neither track nor throw away; I'm

just

Quoted 2 lines

happy to have them and keep them out of version control.

Quoted 8 lines
Quoted 1 line

Unfortunately, there weren't ignored in the old branch. I'd be quite happy

with

Quoted 2 lines

non-destructive switching like "checkout everything what doesn't overwrite an untracked file", so I would end in the old branch with a dirty working tree.

Is

Quoted 1 line

it possible?

No answer?

Quoted 7 lines

Which means nobody is interested in. Well, not really.

I also get irritated by a similar situation, where the untracked files have the same content as the to-be-checked-out files. I have been tempted (but never got around) to make git compare the in-index content and the untracked file, if it's the same, no need to abort the checkout process.

I was asked to provide a working example of the happening, but I haven't managed to reproduce it yet. However, it's not very rare (it just never happen when I need it).

Quoted 3 lines

But your approach may be better. Yes, I think it's possible. Any suggestion for checkout's new argument? --no-overwrite-untracked seems too long.

I would go even further: a switch called "ignorant" or "lenient" allowing to always switch branches in a non-destructible way. All files normally causing abort would be left unmodified, so you could do git checkout --ignorant forth; git checkout back
and would (assuming you started in branch "back") land in the original state without loosing anything. Of course, this means, that the ignorant checkout doesn't lead you into a clean state, but that's why I'd like to use a switch instead of making it the default. :)

I may be talking non-sense as I'm quite inexperienced user, however I'd love an easier way for switching branches. Quite often, I'd like to put a modified file onto a different branch. This sounds probably strange, but my work is really quite chaotic in this respect at the moment and I'd like to organize it better by using a couple of (quite similar) branches (with a lifespan of a few days at most).

#4 lore raw
Quoted 4 lines · On Fri, Nov 5, 2010 at 1:36 PM, Maaartin-1 <[email protected]> wrote:
Quoted 3 lines

But your approach may be better. Yes, I think it's possible. Any suggestion for checkout's new argument? --no-overwrite-untracked seems too long.

Quoted 8 lines

I would go even further: a switch called "ignorant" or "lenient" allowing to always switch branches in a non-destructible way. All files normally causing abort would be left unmodified, so you could do git checkout --ignorant forth; git checkout back
and would (assuming you started in branch "back") land in the original state without loosing anything. Of course, this means, that the ignorant checkout doesn't lead you into a clean state, but that's why I'd like to use a switch instead of making it the default. :)

One thing to consider. If there are conflicts, I don't think we should allow this "ignorant" mode. That would mess up work tree in a bad way. And because it would leave worktree in a dirty state, maybe --dirty-worktree is suggestive.

Signature · 1 line
Duy
#5 lore raw
In reply to Maaartin-1

"Maaartin-1" <[email protected]> wrote in message news:[email protected]...

Quoted 49 lines · Nguyen Thai Ngoc Duy
Quoted 23 lines · Maaartin
Quoted 13 lines
Quoted 5 lines · Maaartin <grajcar1 <at> seznam.cz> writes:

I sometimes run in a problem similar to
http://kerneltrap.org/mailarchive/git/2008/10/15/3667644/thread
There are some ignored files which I want neither track nor throw away; I'm

just

Quoted 4 lines

happy to have them and keep them out of version control.

Unfortunately, there weren't ignored in the old branch. I'd be quite happy

with

Quoted 8 lines
Quoted 4 lines

non-destructive switching like "checkout everything what doesn't overwrite an
untracked file", so I would end in the old branch with a dirty working tree.

Is

Quoted 1 line

it possible?

No answer?

Quoted 7 lines

Which means nobody is interested in. Well, not really.

I also get irritated by a similar situation, where the untracked files have the same content as the to-be-checked-out files. I have been tempted (but never got around) to make git compare the in-index content and the untracked file, if it's the same, no need to abort the checkout process.

I was asked to provide a working example of the happening, but I haven't managed to reproduce it yet. However, it's not very rare (it just never happen when I need it).

Quoted 3 lines

But your approach may be better. Yes, I think it's possible. Any suggestion for checkout's new argument? --no-overwrite-untracked seems too long.

I would go even further: a switch called "ignorant" or "lenient" allowing to always switch branches in a non-destructible way. All files normally causing abort would be left unmodified, so you could do git checkout --ignorant forth; git checkout back
and would (assuming you started in branch "back") land in the original state without loosing anything. Of course, this means, that the ignorant checkout doesn't lead you into a clean state, but that's why I'd like to use a switch instead of making it the default. :)

Quoted 7 lines

I may be talking non-sense as I'm quite inexperienced user, however I'd love an easier way for switching branches. Quite often, I'd like to put a modified file onto a different branch. This sounds probably strange, but my work is really quite chaotic in this respect at the moment and I'd like to organize it better by using a couple of (quite similar) branches (with a lifespan of a few days at most).

You could use git-stash in combination with git-clean. Let's say BranchA has FileA (untracked), and BranchB has FileA (tracked):
git branch = BranchA
git stash = this will store your dirty working tree in a commit behind-the-scenes, and store your index in a commit behind-the-scenes, and then run a git-reset --hard behind the scenes.
git status = is FileA still 'untracked'?
if so, git clean -f to remove it. don't worry, its in your stash you
just made.
git clean -f
git checkout BranchB
now you have FileA from BranchB
git checkout BranchA
git stash apply, (use git stash apply --index if you want the index back also)
now you have BranchA and its untracked FileA

Using the stash and clean will also keep various untracked files from different lines-of-development from accumulating in your worktree. "Keep track" of your untracked files by stashing them away and cleaning them up, so to speak. This is actually pretty straightforwarad and easy once you get used to it, IMHO.

v/r,
Neal