Hi,

I am learning Git system these days. I have made a new folder in ubuntu and in terminal did this:

  1. git init
  2. then made a file testfile1.txt
  3. then made a branch using "git branch newbranch"
  4. then checkout to this branch.
  5. then, made a new file in the branch testfile2.txt and added some content.
  6. then i switched to master branch.
  7. then "ls". there also I was able to see this file. (BIG DOUBT, why I saw this file in master branch.)
  8. Then I switched again to newbranch and "git add ." --> "git commit" then again i switched to the master branch and then "ls".
  9. Now, I was not able to see testfile2.txt but obviously it was there in newbranch.

Can you help me visualize that why I was able to see the file till I didn't commit the file? Please help.

If a file has never been added, it is untracked. Untracked files are not affected by anything you do with git (like switching branches).

One way of looking at it is that, until you added the file, the file did not exist in any branch. Instead it existed in the directory and it kept existing there no matter which branch you were in because it was completely independent from git. Once you added the file, it became tied to git and now existed in some branches only, so its existence would depend on which branch you're in.

@sepp2tk nice explanation. Can you tell me onw thing why we have to commit? The file in which i am making changes is in local repository. right? I read somewhere that workspace is different from the local repository. How? I mean when I made changes in testfile1.txt. it is in a folder. Isn't it my local repo only?

What is need of committing? I know there is one .git hidden folder. I checked into that. There is no copy of testfile1.txt present. When I do Ctrl + S, then it is saving my file. So what is committing? Do I have two copies of the same file in git system?(one i am editing and one i over-write after commoting) ? Explain.

Can you tell me onw thing why we have to commit?

To apply changes from the workspace to the repository.

I read somewhere that workspace is different from the local repository. How?

The workspace contains one specific version of your files. The repository (i.e. the .git folder) contains a complete history which keeps track of all changes you ever commited with branches and everything.

I know there is one .git hidden folder. I checked into that. There is no copy of testfile1.txt present.

There is, it just doesn't exist as a file as such. Rather you can think of the repository as a database which keeps track of all actions you ever committed (like creating files, changing, moving or deleting them). It contains all information necessary to undo and redo any change you every committed as well as combining changes from different branches.

Your workspace on the other hand only contains the contents of the files right now.

Do I have two copies of the same file in git system?(one i am editing and one i over-write after commoting) ? Explain.

You can sort of think of it as having one copy for each version that ever existed. Like if you create a file with the contents "bla", commit it, change it to "blablubb", commit it again and then change it to "blobbel", your workspace will contain a file with the contents "blobbel". Your repository will (sort of) contain the versions containing "blablubb" and "bla". And once you commit "blobbel", it will contain that as well.

Technically it doesn't actually store copies of each version of the file, but rather just the diffs, containing the changes made to the files. But either way it allows you to view any version of the file that has ever been committed.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.