5

I'm honestly a Git novice so pardon me if I'm overcomplicating things ehre.

1.) I have file A in my working directory that has 10 lines. the same file is in remote repo too with 8 lines

2.) When I pull the file A from remote repo, git says "your branch is up to date with origin/remote-branch".

Step 2 did not get rid of the changes I have in my local but Git does not complain about additional change that I have in my local either. To me, it looks like I have all the updates from remote. Let's say a couple days after I make change to another file B (from the same branch) and try to commit them by using "git add ." the 3 additional lines that were in my file A is also now pushed to the remote inadvertently. Of course git status would have shown me that there are 2 modifications but if I forget about the change to file A from 2 days ago and if I were to think that file B is the only change I did just now and did a "git add ." then I'm in trouble.

My question really is, is git checkout -- fileA the only way to ensure I exactly match the remote fileA with my fileA? I also see git pull -f but that's risky to do. What's the safest way for me to know that git pull still did its job while there are some additional changes I have in my working directory? Should not git stop me from even pulling? What's weird is sometimes it does say "your local changes could be lost with the pull" but it does not do that all the time when the file is reasonably clean and if git pull would not impact any of my local changes. I'm just trying to see how do I avoid such error with inadvertently pushing something to the repo that I was just experimenting with a file in a branch. I was hoping pull would either clean it up or error out always but it does not seem to be true.

Thanks for your help!

Zoe
  • 27,060
  • 21
  • 118
  • 148
JavaNovice
  • 1,083
  • 1
  • 12
  • 20
  • The "duplicate" question doesn't answer the the asker's questions. It also doesn't clear up the confusion that is evident in the questions that are asked. The underlying question is "Why does git pull work this way?" Hopefully you're not struggling with this anymore... I hope you found some good resources to answer your question. – Neal Gokli Oct 26 '18 at 22:27
  • @Neal Gokli unfortunately I still haven't found an answer to this question. If you could help that'd be great. – JavaNovice Oct 28 '18 at 04:16
  • I suggest finding a git tutorial! Unfortunately, I can't recommend one (my kind coworker taught me the basics). For your question: 1) `git add` doesn't push to remote! It adds to a local staging area. `git commit` commits staged changes to a new commit in your local repo. `git push` pushes local repo commits to remote. 2) `git pull` brings remote changes into your local repo. It's specifically meant to avoid overwriting your local repo and working directory, unless you force it, because that's dangerous! – Neal Gokli Nov 15 '18 at 20:17
  • 3) `git checkout` can reset the tracked files in your working directory to match a commit in your local repo. By default, it matches the commit you are working from, but can also change to a different commit/branch. It will not touch untracked files (files not saved in the commit in the local repo). Unless you specify a path, it won't overwrite your working directory changes. 4) `git status` will show you the status of your working directory, including tracked and untracked changes. Use it often to be sure that it is what you expect! – Neal Gokli Nov 15 '18 at 20:24
  • 5) This is all meant to avoid losing any changes by accidentally running a command at the wrong time, when you haven't slept for 15 hours! 6) This is just off the top of my head. I didn't look at documentation while writing this. So take it with a grain of salt. There are many flags that can change functionality (like the `-f` you mentioned!). – Neal Gokli Nov 15 '18 at 20:26

0 Answers0