I’ve started to learn git in the past few days and I have a basic question.
Imagine this situation:
You create an empty folder and in there you create a foo.txt file.
In order to push the data into the remote repository (e.g. github), you make the following commands:
git init-> in order to initalize the repository in the foldergit add .-> in order to add to the index all the files in the folder, in this case foo.txtgit commit -m "First commit"-> We make the first commitgit remote add origin git@github.com:username/gitrepository.git-> We add the github repository-
git push origin master-> We push the changes to the remote repository-
Now in the github repository you have only a foo.txt file.
-
So now you are in another machine and want to use the repository, so you create an empty folder and pull the data from the remote repository.
Then you create a file named bar.txt, delete foo.txt, add the file (bar.txt) to the index, make a commit and push the changes to the remote repository. -
Now in the repository we have only the bar.txt file
-
But now in the first machine we still have the foo.txt file, and if we make a pull from the remote repository we have foo.txt and bar.txt.
-
But that’s not what I would like to do, I would like to pull all the files from the repository and work only with those files. So in this example, if I only have now bar.txt in the repository, when I make a pull I don’t want any other files in my project folder.
How do you manage that?
I think you missed a step in 2.
You have to remove the foo.txt file from the index. It requires a specific command (“git add .” only puts file to the index but doesn’t remove files) :
Then commit.
At step 3, after a pull on the first computer, the foo file should have disappeared.