I often face this problem with git: I clone a git repo of some project. Then I make changes to some source file, but I don't commit it, because my work is not complete. But I have to keep updating my repo to make it in sync with the master. So I do a 'git pull', so that my repository is up-to-date and so I can produce my patch against the latest repo. Now suppose I edit the file hello.c in my local clone (not yet committed) and someone else has made changes to the same file and committed it to the master repo. Now when I do 'git pull' the other person's changes get added to hello.c and the changes that I had made, get deleted. This is the natural behavior obviously. Is there a way to tell git to only 'add' changes and not 'subtract' anything? Or something to that effect? I don't want my changes to be deleted when I do a git pull, but also I want to work with the latest repository.
Asked
Active
Viewed 1,440 times
1
-
3So commit your code. Why aren't you doing local commits? You must do that before a pull. – Greg Hewgill Mar 20 '12 at 04:47
-
1Work on a branch so that your work won't be affected by what happens on the main code lines until you decide you want it to be affected? – Jonathan Leffler Mar 20 '12 at 05:28
-
Read this topic also - [What's the difference between git pull and git fetch?](http://stackoverflow.com/questions/292357/whats-the-difference-between-git-pull-and-git-fetch) – Lazy Badger Mar 20 '12 at 05:44
1 Answers
1
- As Greg said - "Commit!". "Commit often, commit fast" is common rule in VCS-world for years
- Use correct commands for your tasks. If you don't know all behind the scene of
git pull
- read FM! Learn difference between pull and fetch or see at examples in Git Reference
The second command that will fetch down new data from a remote server is
git pull
. This command will basically run a git fetch immediately followed by a git merge of the branch on that remote that is tracked by whatever branch you are currently in. I personally don't much like this command - I prefer running fetch and merge seperately. Less magic, less problems.
- Use good, bullet-proof workflow. Quote from online git-pull(1) Manual Page
You never do your own development on branches that appear on the right hand side of a colon on Pull: lines; they are to be updated by git fetch. If you intend to do development derived from a remote branch B, have a Pull: line to track it (i.e. Pull: B:remote-B), and have a separate branch my-B to do your development on top of it.

manojlds
- 290,304
- 63
- 469
- 417

Lazy Badger
- 94,711
- 9
- 78
- 110