Same user 2 computers, changes lost instead of merged

I am pushing to github from the same Windows Domain and same github account but two different computers (I have a laptop-with-vpn at home, and a Desktop at work).

I seem to be losing my changes. Git seems to think that each submission of the file is a request to overwrite it with a new edition (instead of merging the home-submission’s changes with those of the work-submission).

So if I add 5 lines at work.

Then I go home and add 2 lines at home. 

I should have 7 new lines.

Instead I end up with 2  new lines. I’ve lost all my at-work changes.

Suggestions anyone please?

The commands  I use are:

git commit -a -m  “today’s changes”

git fetch origin master

git merge origin/master

git push origin master

I think I can solve this by having a 2nd github account included in the project as a collaborator.

If anyone  knows a cleaner way to resolve this, please let me know. 

To test this I:

  1. Created this commit creating janthonyl.txt as a blank file using the GitHub web UI
  2. Issued git pull on my local machine with the master branch checked out to put both the GitHub repository and my local repository in the same state
  3. Created this commit adding five lines to the janthonyl.txt file using the GitHub web UI
  4. Added two lines to the local copy of janthonyl.txt using my favorite editor
  5. Issued the command git commit -a -m "Today's changes" locally to commit the local changes
  6. Issued the command git fetch origin master locally to get the changes from the GitHub repository
  7. Issued the command git merge origin/master locally

This resulted in the following error message:

Auto-merging janthonyl.txt
CONFLICT (content): Merge conflict in janthonyl.txt
Automatic merge failed; fix conflicts and then commit the result.

And returned exit code 1, signaling an error.

If you execute git status at this point, you would see:

On branch master
Your branch and 'origin/master' have diverged,
and have 1 and 1 different commits each, respectively.
  (use "git pull" to merge the remote branch into yours)

You have unmerged paths.
  (fix conflicts and run "git commit")
  (use "git merge --abort" to abort the merge)

Unmerged paths:
  (use "git add <file>..." to mark resolution)

        both modified: janthonyl.txt

no changes added to commit (use "git add" and/or "git commit -a")

And the way Git is configured by default, executing git push origin master at this point results in this error message:

To https://github.com/lee-dohm/test-repo
 ! [rejected] master -> master (non-fast-forward)
error: failed to push some refs to 'https://github.com/lee-dohm/test-repo'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

And again, exits with error code 1. Git does all of this specifically to prevent you from losing data.

The only way that you could get Git to behave like you’re describing is if instead of git push origin master you executed git push --force origin master, which tells Git that you know what you’re doing and you’re ok with overwriting data. There are probably ways to configure Git to always issue a force push in certain circumstances, but again, this is not how Git behaves by default.

The proper thing to do when you see that merge conflict is to resolve the conflict. You’ll notice that the contents of the file at this point are:

<<<<<<< HEAD
Add two lines at home
2
=======
Add five lines at work
2
3
4
5
>>>>>>> origin/master

If you want all seven lines, then you could edit the file to look like this:

Add five lines at work
2
3
4
5
Add two lines at home
2

Then:

  1. Execute git add --all
  2. Execute git commit
  3. Accept the default merge commit message
  4. Execute git push origin master

Which succeeds, as can be seen in the history of the janthonyl.txt file in my test repo.

Also, to show that if there are no merge conflicts that things wouldn’t behave the way you’re describing, I created another test with janthonyl2.txt. This time I built a file  that had some text that would separate the two changes so that Git’s automatic merge resolution could figure out that the two changes didn’t conflict and handle things for you. So I:

  1. Created janthonyl2.txt through the GitHub web UI
  2. Added some lines to the file to separate the two changes
  3. Executed git pull on my local machine to ensure that the remote and local repositories were in the same state
  4. Added the five lines at the top of the file (before the block) through the GitHub web UI
  5. Added the two lines at the bottom of the file (after the block) locally using my favorite text editor
  6. Executed git commit -a -m "Today's changes" locally
  7. Executed git fetch origin master locally
  8. Executed git merge origin/master locally

This time I didn’t get an error, I got the following message:

Auto-merging janthonyl2.txt
Merge made by the 'recursive' strategy.
 janthonyl2.txt | 5 +++++
 1 file changed, 5 insertions(+)

Notice the text “Merge made by the ‘recursive’ strategy.”, confirming that the merge was automatically resolved. Also, there was no error code returned.

I then was able to execute git push origin master resulting in the file history I linked to at the top of this post.

1 Like