Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 folder
  • git add . -> in order to add to the index all the files in the folder, in this case foo.txt
  • git commit -m "First commit" -> We make the first commit
  • git remote add origin [email protected]:username/gitrepository.git -> We add the github repository
  • git push origin master -> We push the changes to the remote repository

    1. Now in the github repository you have only a foo.txt file.

    2. 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.

    3. Now in the repository we have only the bar.txt file

    4. 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?

like image 201
rfc1484 Avatar asked Apr 24 '11 21:04

rfc1484


2 Answers

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.

like image 177
Benoit Courtine Avatar answered Sep 23 '22 17:09

Benoit Courtine


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.

like image 24
Oerd Avatar answered Sep 24 '22 17:09

Oerd