1

Four of our members are working on a Git branch called "Dev". I have completed some of my work and when trying to push it, it gives me an error because someone has already made a commit/push to "dev". It says to pull first and push, but when I did pull my modified data was lost. However, when I switched to using master branch, I recovered.

How I am going to push my changes to "dev" without removing changes from other developers as well?

random
  • 9,774
  • 10
  • 66
  • 83
Dilukshan Mahendra
  • 3,230
  • 7
  • 41
  • 62
  • Do a merge. Git's merging capabilities seem poor though so prepare yourself for conflicts – James Oct 06 '13 at 11:34

2 Answers2

0

You shouldn't lose your work if you add it to the index first, and commit it, before the git pull.

git pull will fetch and merge the upstream dev branch or the upstream repo 'origin' to your current branch 'dev'.

But if you didn't commit, then your changes would be "lost".

Except you can still find them with a:

git fsck --lost-found  => give a SHA1
git cat-file -p SHA1 => see the content of your lost work

See "Git pull deleted uncommitted changes" for more.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Hi VonC, can you please tell me how I am gonna add it to the index first? I am new to Git. – Dilukshan Mahendra Oct 06 '13 at 13:55
  • @DilukshanMahendra by using the [command `git add`](http://git-scm.com/docs/git-add), as described in http://git-scm.com/book/en/Git-Basics-Recording-Changes-to-the-Repository – VonC Oct 06 '13 at 13:56
  • @DilukshanMahendra note that the index is a very important notion to understand: see my old answer http://stackoverflow.com/a/3690796/6309 – VonC Oct 06 '13 at 13:57
0

You can use git stash in this scenario.

#git stash
#git pull
#git stash pop / git stash apply <stash id> (you can look up the stash id with `git stash list`)

In generally, git will auto merge the code if you don't modify the same line. Otherwise you should merge the code manually somtimes, then you can commit and push your code

user1179442
  • 473
  • 5
  • 16