0

I create a new remote repository in my LAN and I add the projects (PROJECT 1PROJECT 2).

USER A and USER B clone the repository at a time and USER A did the changes in PROJECT 1 and pushed these changes.

USER B did the changes in PROJECT 2,he didn't make any changes in PROJECT 1 and while pushing the data also he select only PROJECT 2 content, But it gives error while pushing.

ERROR :

error: failed to push some refs to http://gitbub.com
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first merge the remote changes (e.g.,
hint: 'git pull') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.`

How to allow the push for User 2?

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
user3619128
  • 285
  • 1
  • 5
  • 12

1 Answers1

2

As commented by jcm, the easiest solution would be for project1 and 2 to be sotred in their own repos.

But if they are in the same repo, then:

User2 still need to rebase his/her work on top of an updated repo.

git pull --rebase

Compared to git pull alone, this allows you to replay your local commits on top of the updated remote tracking branch.

x--x--x--x--y--y--y (master): y means Project2 commits
         |
  (origin/master)

git pull --rebase = git fetch + git rebase

git fetch means:

x--x--x--x'--x' (origin/master)
       \
        y--y--y

git rebase means:

x--x--x--x'--x'--y'--y'--y' (master)
             |
      (origin/master)

Now a git push will be a trivial one, adding your local commits on top of origin master.

Since User2's work was separate from User1's, the rebase will trivially replay User2's commit on top of origin/master, and will allow the (fast-forward) push.

Since both projects are in the same repo, that situation will arise if they are modified on the same branch.

A good workaround would be to create two branches, one for each project.

If you need both project, then you need to pull --rebase before pushing.
You can consider making the rebase automatic on a git pull.


Multiple git pull alone would introduce merge commits which are not needed here (since the modifications are done on two separate sets of files)

x--x--x--x'----x' (origin/master)
       \        \
        y--y--y--m (master)

Then the next pull:

x--x--x--x'----x'--x'---x' (origin/master)
       \        \        \
        y--y--y--m--y--y--m (master
Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250