3

I thought I know simple features of git until I began work with other contributors:)

The situation is as follows:

  1. I pulled source code from repository (from commit: 2c49868).
  2. I changed some of the code and I noticed that the changed app doesn't worked. I didn't use git commit and I didn't use git push.
  3. I would like to restore the source code before the change (from commit: 2c49868).

What should I do to not spoil the repo?

I'm going to do:

git fetch --all
git reset --hard origin/master

Is it correct or maybe is better way to do this?


EDIT:

It turns out that:

  1. I pushed the source code with new commit: (c6a7e5f) on the service computer at work yesterday.
  2. I change some of the code locally (I didn't use git commit and I didn't use git push.
  3. I went home.
  4. I pulled the source code (from commit: c6a7e5f) on the another, personal computer.
  5. I changed the source code and added/deleted some of files at home, used git commit (commit: 2c49868) and git push.
  6. Today, I came back to work (to service computer). In my IDE I see the changes from the second point and I would like to have the source code from the fifth point (commit: 2c49868).

In conclusion: I don't want to have local changes (from the second point) on the service computer because they are still here, but I want to have the newest source code from this repo on the service computer.

I am asking for your understanding.

plkpiotr
  • 345
  • 2
  • 9
  • 23
  • Please don't write a new question ... state your problem precisely and your repo "situation" – YesThatIsMyName Jul 13 '18 at 07:39
  • 1
    Do you also use some git client or IDE? It might be easier to use those. – Rufi Jul 13 '18 at 07:39
  • Yes, I use IntelliJ IDEA, but I'm editing the post because the situation is a little different. – plkpiotr Jul 13 '18 at 07:43
  • 1
    finding it hard to picture here, so of your commit hashs which ones do you NOT want? @plkpiotr – Josh Stevens Jul 13 '18 at 08:11
  • Here is the history: https://github.com/plkpiotr/fifa-backend/commits/2c49868498a7cade15a16eb8bb08d88d5b3a95b7 I don't want to have local changes (from the second point) on the service computer because they are still here, but I want to have the newest source code from this repo on the service computer. – plkpiotr Jul 13 '18 at 08:16

2 Answers2

2

I think you should revert the changes. Then you will come to the last commit. I think you do it correctly, but fetch is unnecessary.

In case you want to include folders have look into this post, or this one.

I would also copy my changes into local text editor before doing reverting. In case you are using Intellij IDEA I advise to shelve your changes. That means that your local changes will be saved and you will come to the last pushed commit. Shelving and unshelving are well described in Jetbrains documentation.

Rufi
  • 2,529
  • 1
  • 20
  • 41
  • Thanks for reply ; ) I could sure use your tip in the future, but I noticed that situation in my repo is a little different. I will create a new question to not mislead. – plkpiotr Jul 13 '18 at 07:36
  • Thank you for additional tips :) – plkpiotr Jul 13 '18 at 08:00
2

if you did not commit it there are a few things you can do

git stash 

This will stash your changes and bring you back to the correct source - you may want them later.

or git reset --hard

This will wipe out differences in the working tree (you can not get your changes back)

If you want to clear any untracked files git reset will not be enough and you have to use

git clean -f

This will remove any files from the tracked root directory that are not under Git tracking.

There is no need to do a fetch beforehand if you are up to date.

If you did commit it (i am assuming you did it in 1 commit) you can do

git reset --hard HEAD^

This will reset the last commit and remove it completely (use hard with caution and if you are worried use soft and then revert once you checked what it has rechecked out.)

Josh Stevens
  • 3,943
  • 1
  • 15
  • 22
  • I think he commited but not pushed ("did't send commit")... I also was not sure, I think Rufi is right. – YesThatIsMyName Jul 13 '18 at 07:31
  • 2
    I gave him answers for both just incase – Josh Stevens Jul 13 '18 at 07:34
  • Thanks for reply ; ) I could sure use your tip in the future, but I noticed that situation in my repo is a little different. I will create a new question to not mislead. – plkpiotr Jul 13 '18 at 07:35
  • Thank you for further tips :) – plkpiotr Jul 13 '18 at 08:01
  • `git reset --hard` and `git pull` helps me ; ) In this way I got source code from the last commit : ) – plkpiotr Jul 13 '18 at 11:09
  • sorry, I just seen your comment, are you still stuck? – Josh Stevens Jul 13 '18 at 11:19
  • No :) Everything is ok ;) I could create new folder and use `git clone` or `git init` and pull thanks to TortoiseGit, but I wanted to work in the same project, in the same folder ;) Because of the fact I noticed that I should remind some information about git. Anyway, thanks, for your help :) – plkpiotr Jul 13 '18 at 11:55