0

I have a local folder on my computer "local_d"

I have a repo with 2 branches "master", "hipster"

I do some operations on "local_d" with a script and want to copy this "local_d" to the "hipster" branch

I am currently active in the (*hipster) branch but outside the git repo (e.g. home folder)

When I do:

cp -r ~/local_d ~/git/repo

"local_d" exists both in "master" and the "hipster" branches???

Would appreciate any help on this.

Guguma
  • 93
  • 1
  • 8

1 Answers1

0

"local_d" exists both in "master" and the "hipster" branches???

No. It's not in any branches. It's not in the repository at all.

"But I can see it, it's right here", I hear you say. Yes. The thing is "right here" is not in the repository. The files you can see and work with are not in Git.

This is probably the very first thing you should know, before you start using Git. Git does not store files: Git stores commits. Commits then contain files, but not in their ordinary, every-day format. The files stored inside Git commits are in a special, read-only, Git-only, compressed and de-duplicated form, and nothing but Git can read them and nothing—not even Git itself—can overwrite them. This makes them quite useless for getting work done. (This sort of thing, where the version-controlled files can't be used directly, is true of many version control systems: Git is not particularly special here.)

What this means for you is that in order to use files from some Git commit, you first have Git copy those files, extracting them out of the commit, changing them from their internal Git-only format into files that you can see and use. These plain ordinary everyday files, which are useful for getting work done, are not the files in the commit. They're just plain ordinary everyday files.

The committed files are safely tucked away inside the commits. Git works with the commits. Git can also make new commits, and to do that, you'll want to have Git copy any updates you made. To get those updates into Git you must use git add on those files.1

What this means for you is that whatever you do to the copied-out, useful files in your work area, that's not in Git. Copying a bunch of files into your working tree is fine, but it doesn't put them into anything other than your working tree. Git doesn't save them anywhere, but it doesn't remove them either. They're yours, not Git's. Git just leaves them there.

There are some files in your work area that did come out of Git, when you ran git checkout (or in Git 2.23 and later, git switch). When you pick some other commit to git checkout, Git takes those files out of your work area, and puts in the files from the other commit instead. As long as all the local_d/* files aren't in either commit, though, they just keep sitting there.

Once you use git add to tell Git: these files should go into the next commit I make—this copies them into what I like to refer to as your proposed next commit; note that you haven't actually made this commit yet—now Git knows about these files and will start doing things with them. But until then, Git just assumes that you made this extra stuff and would like to keep it there while Git works around it.


1While there are some shortcuts for this, I believe it's not a good idea to learn them first, for multiple reasons that I won't go into here.

torek
  • 448,244
  • 59
  • 642
  • 775