1

I'm new to git, I was building a project with couple of branches. I was in branch B with uncomitted changes and then I run this command:

git checkout -b C

to move to new branch, let's call it branch C.

In branch C I uncommit those changes and since then I can't get those changes back, even if I go back to branch B.

I'm not sure if I checkout of a branch it's means that it's saved somewhere.

Is there a way to fix it?

SwissCodeMen
  • 4,222
  • 8
  • 24
  • 34
Arik Jordan Graham
  • 301
  • 1
  • 3
  • 9
  • 4
    if you checkout a new branch you take with you the changes made in the files, so there is no way to get them back – rioV8 May 15 '21 at 17:49
  • 1
    Does this answer your question? [How do you undo "Discard all changes" in VS Code/Git](https://stackoverflow.com/questions/43541167/how-do-you-undo-discard-all-changes-in-vs-code-git) – SwissCodeMen May 15 '21 at 22:07
  • What does "I uncommit those changes" mean? – matt May 15 '21 at 23:09
  • @matt when i said "uncommit" i meant that i did not commit the changes , so basicly i did nothing before checkout to a new branch – Arik Jordan Graham May 16 '21 at 12:08

1 Answers1

0

Git doesn't track changes you haven't told it to track.

All changes that are uncommitted are just sort of floating. When you use checkout those changes come with you to the new branch. If you destroy those changes without saving them, they are gone.

If you want to save the changes on a branch to come back to later, you can use git stash, this saves the uncommitted changes at the current commit and cleans them away from your current perspective.

unDeadHerbs
  • 1,306
  • 1
  • 11
  • 19
  • Git doesn't "track changes" _at all_. It stores commits. – matt May 15 '21 at 23:08
  • To my knowledge, in git, commits are stored as diffs; or, in common English, they are changes, that are kept track of. – unDeadHerbs May 15 '21 at 23:11
  • When was that changed and what does git do nowadays? – unDeadHerbs May 15 '21 at 23:17
  • It was never changed. Git has always been about commits, which are snapshots. Might want to see my https://www.biteinteractive.com/picturing-git-conceptions-and-misconceptions/, see the Commits section. – matt May 15 '21 at 23:19
  • Thanks, I'm more familiar with git's origins than it's development. To my recollection git was originally (before it was named) a way of bundling patch files and emailing them. That was latter changed to ssh; but, the way of thinking about commits as changes from the previous commit does a good job of demystifying how merges, cherry picking, and history modding operations work and behave. – unDeadHerbs May 15 '21 at 23:24
  • I disagree. Merges etc do rely on diffs but commits are not diffs. Diffs are derived from comparison of commits; they are not what Git stores. Saying they are does the OP no favors. – matt May 15 '21 at 23:31
  • Feel free to edit/improve my wording. You are clearly more informed on the modern behind the scenes than I. – unDeadHerbs May 15 '21 at 23:36