Git Major Issues

Let’s consider, we have two copies of a git repo on two 
servers. One is on server “managed1” and “managed2”.

The first issue occurred when you have made changes on managed1
and pushed it to remote. But on managed2 you haven’t pull the
latest repo and committed the changes. It will give you error.
On managed1:

[varelite1@managed1 repo]$ ls
index.html  managed1.txt  managed2.txt  varelite.txt

[varelite1@managed1 repo]$ echo "code1" > code1

[varelite1@managed1 repo]$ ls
code1  index.html  managed1.txt  managed2.txt  varelite.txt

[varelite1@managed1 repo]$ git add code1

[varelite1@managed1 repo]$ git commit -m "code1"
[master 2eb7caf] code1
 1 file changed, 1 insertion(+)
 create mode 100644 code1

[varelite1@managed1 repo]$ git push origin master
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 261 bytes | 52.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To github.com:somethingtostudy12/managed1.git
   0d8b1a8..2eb7caf  master -> master

[varelite1@managed1 repo]$ ls
code1  index.html  managed1.txt  managed2.txt  varelite.txt


On managed2:

[varelite2@managed2 managed1]$ ll
total 16
-rw-rw-r--. 1 varelite2 varelite2 94 May 23 10:37 index.html
-rw-rw-r--. 1 varelite2 varelite2 10 May 23 11:33 managed1.txt
-rw-rw-r--. 1 varelite2 varelite2  9 May 23 10:41 managed2.txt
-rw-rw-r--. 1 varelite2 varelite2 19 May 18 14:14 varelite.txt

[varelite2@managed2 managed1]$ echo "code2" > code2

[varelite2@managed2 managed1]$ git add code2

[varelite2@managed2 managed1]$ git commit -m "code2"
[master 82e49b4] code2
 1 file changed, 1 insertion(+)
 create mode 100644 code2
Here is the error:
[varelite2@managed2 managed1]$ git push origin master
To git@github.com:somethingtostudy12/managed1.git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'git@github.com:somethingtostudy12/managed1.git'
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.
So here is the issue: remote has some changes which is not 
available at managed2 while managed2 has some changes which are
not available on master at origin. So create a new version
which must have all above commits means you need to do it merge
commit.
This can be done by pulling it repo on managed2.
[varelite2@managed2 managed1]$ git pull origin master
Merge branch 'master' of github.com:somethingtostudy12/managed1

# Please enter a commit message to explain why this merge is necessary,
# especially if it merges an updated upstream into a topic branch.
#
# Lines starting with '#' will be ignored, and an empty message aborts
# the commit.
Issue occured due to no pull

remote: Enumerating objects: 4, done.
remote: Counting objects: 100% (4/4), done.
remote: Compressing objects: 100% (1/1), done.
remote: Total 3 (delta 1), reused 3 (delta 1), pack-reused 0
Unpacking objects: 100% (3/3), done.
From github.com:somethingtostudy12/managed1
 * branch            master     -> FETCH_HEAD
Merge made by the 'recursive' strategy.
 code1 | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 code1
Now run “git log” command to see the commit logs.
[varelite2@managed2 managed1]$ git log
commit 442befd1b63cd3739625a39c346e94cc48a9711e
Merge: 82e49b4 2eb7caf	(these commits are of code1 and code2)
Author: varelite2 <managed2@kb.com>
Date:   Thu May 23 11:44:16 2024 +0530

    Merge branch 'master' of github.com:somethingtostudy12/managed1

    Issue occured due to no pull

commit 82e49b4bd5ff40d458610418d4d74d01511e4aad
Author: varelite2 <managed2@kb.com>
Date:   Thu May 23 11:43:52 2024 +0530

    code2

commit 2eb7caf9551da06f6e6e2283a32d1078e8d4cc9d
Author: varelite1 <varelit1@kb.com>
Date:   Thu May 23 11:43:04 2024 +0530

    code1
Now you can push to origin so that origin repo will up-to-date.
[varelite2@managed2 managed1]$ git push origin master
Counting objects: 7, done.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (5/5), 506 bytes | 0 bytes/s, done.
Total 5 (delta 2), reused 0 (delta 0)
remote: Resolving deltas: 100% (2/2), completed with 1 local object.
remote: To git@github.com:somethingtostudy12/managed1.git
   2eb7caf..442befd  master -> master
Now we will go ahead with another scenario where on both the 
server same file will be created or modified.
On managed1:

[varelite1@managed1 repo]$ ls
code1  code2  index.html  managed1.txt  managed2.txt  varelite.txt

[varelite1@managed1 repo]$ git status
On branch master
nothing to commit, working tree clean

[varelite1@managed1 repo]$ vi index.php

[varelite1@managed1 repo]$ git add index.php

[varelite1@managed1 repo]$ git commit -m "issue2"
[master 36a4f95] issue2
 1 file changed, 1 insertion(+)
 create mode 100644 index.php

[varelite1@managed1 repo]$ git push origin master
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 266 bytes | 88.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To github.com:somethingtostudy12/managed1.git
   442befd..36a4f95  master -> master


On managed2:

[varelite2@managed2 managed1]$ ls
code1  code2  index.html  managed1.txt  managed2.txt  varelite.txt

[varelite2@managed2 managed1]$ git status
# On branch master
nothing to commit, working directory clean

[varelite2@managed2 managed1]$ vi index.php

[varelite2@managed2 managed1]$ git add index.php

[varelite2@managed2 managed1]$ git commit -m "case2-managed2"
[master 0ed6800] case2-managed2
 1 file changed, 1 insertion(+)
 create mode 100644 index.php
Here is the error we are getting:
[varelite2@managed2 managed1]$ git push origin master
To git@github.com:somethingtostudy12/managed1.git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'git@github.com:somethingtostudy12/managed1.git'
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.
[varelite2@managed2 managed1]$ git pull origin master
remote: Enumerating objects: 4, done.
remote: Counting objects: 100% (4/4), done.
remote: Compressing objects: 100% (1/1), done.
remote: Total 3 (delta 1), reused 3 (delta 1), pack-reused 0
Unpacking objects: 100% (3/3), done.
From github.com:somethingtostudy12/managed1
 * branch            master     -> FETCH_HEAD
Auto-merging index.php
CONFLICT (add/add): Merge conflict in index.php
Automatic merge failed; fix conflicts and then commit the result.
If you will the see the below command then you can see the file 
which is having issue. It has both the content which is showing
the merge conflict.
[varelite2@managed2 managed1]$ git status -s
AA index.php

[varelite2@managed2 managed1]$ cat index.php
<<<<<<< HEAD
delhi
=======
hello world
>>>>>>> 36a4f95c32fc4142bb9c1d736bcd6efd234a40ce
Here you need to check with user from managed1 that the changes 
he has made needed or not. You need to the required changes
manually. Here user1 has said that the changes made by him can
be removed by manually editing it.
varelite2@managed2 managed1]$ vi index.php

[varelite2@managed2 managed1]$ cat index.php
delhi

[varelite2@managed2 managed1]$ git add index.php

[varelite2@managed2 managed1]$ git commit -m "fixing merge conflict"
[master 3b10806] fixing merge conflict

Leave a Reply

Your email address will not be published. Required fields are marked *