How can I throw away local changes in my Git repository when pulling a new version from github?
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"
-> W开发者_Go百科e make the first commitgit remote add origin git@github.com:username/gitrepository.git
-> We add the github repositorygit push origin master
-> We push the changes to the remote repositoryNow 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) :
- git rm foo.txt
- git add -u (removes from the index all files that are not anymore in the workspace)
Then commit.
At step 3, after a pull on the first computer, the foo file should have disappeared.
The way git works you will always have those files in your repository (i.e. in your repository's .git
folder). The difference is that in step 4. once you pull changes, you are still left with your original working copy (git will only merge automatically if you have made no changes). I guess you are one merge away from what you want :)
I find this Git Cheat-sheet very helpful.
精彩评论