curious_techy curious_techy - 3 months ago 11
Git Question

new to git : git push giving errors

I'm new to GIT and posting this after trying lots of things.

Issue is simple : I've a file say 'A' in master, which is also present in other branch say 'test'. in both files, there are change and I've to merge the changes

I know there are plethora of earlier posts, which community could guide me to but I've a specific problems which I'm unable to resolve:

so here are thing things I've tried


  1. My current branch is branch 'test' and file on local system(in git directory) is showing changes from that branch.Now I've to merge changes from master so I switched to master using git checkout command

  2. Then, Used git pull origin master and also Git merge test, as I've to merge changes from master to 'test' branch. On trying, Both command shows that there is conflict in file 'A'(e.g of error : merge is not possible because of unmerged files. Automatic merge failed etc....)

  3. Then, Search these errors and it was suggested by number of posts that I've to merge the files MANUALLY using some tools like kdiff etc.



Now Here my questions comes up :

I can easily copy changes present in Github (repository) in Master branch and update the file in git local repository(its version is of Branch 'test'). My question is : how git knows that merge is completed? I even did that and again run command 'git push origin master' but quite understandably, it failed saying, tip of current branch is behind.... etc....

I even thought since my local git repository now has updated and final changes(after manually copying changes) let me do force update using git push -f origin master( I know, it shouldn't be preferred way of merging such changes) . Though this command has run fine but doing 'git checkout master' thrown an another error saying file 'A' needs merge- you need to resolve your current index first' .

This let me to think that Git doesn't know that merge has happenned and brings me to same point how git knows I've merged changes manually ???

Another thing pn same issue, I know somehow, I've to compare file A present in local Git repository ( belonging to branch test) to its version present in master in repository.
Question : how can I get both files side by side in local for comparison?

I know it should be done in some manner so that GIT also knows that merge is done and file present in local machine has updated merged changes and ready to be pushed to master

I know it could be one of silly thing I'm missing but seriously not able to find anything after lots of searching

==============================================

EDIT : ADDING ACTUAL COMMANDS as asked by few members.. thanks !



2000@LTDLUK3400 MINGW64 /d/GitAll/LearnGit (newGitBranch)
$ git checkout master
Switched to branch 'master'
Your branch is ahead of 'origin/master' by 1 commit.
(use "git push" to publish your local commits)
 

2000@LTDLUK3400 MINGW64 /d/GitAll/LearnGit (master)
$ git pull origin master
remote: Counting objects: 3, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
From https://github.com/apexkeen/LearnGit
* branch master -> FETCH_HEAD
8682a7a..bed8390 master -> origin/master
Auto-merging Readme.txt
CONFLICT (content): Merge conflict in Readme.txt
Automatic merge failed; fix conflicts and then commit the result.
 
2000@LTDLUK3400 MINGW64 /d/GitAll/LearnGit (master|MERGING)
$ git merge newGitBranch
error: merge is not possible because you have unmerged files.
hint: Fix them up in the work tree, and then use 'git add/rm <file>'
hint: as appropriate to mark resolution and make a commit.
fatal: Exiting because of an unresolved conflict.
 
2000@LTDLUK3400 MINGW64 /d/GitAll/LearnGit (master|MERGING)
$ git config --global --edit
 

 
2000@LTDLUK3400 MINGW64 /d/GitAll/LearnGit (master|MERGING)
$ git push origin master
To https://github.com/apexkeen/LearnGit.git
! [rejected] master -> master (non-fast-forward)
error: failed to push some refs to 'https://github.com/apexkeen/LearnGit.git'
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.
 
2000@LTDLUK3400 MINGW64 /d/GitAll/LearnGit (master|MERGING)
$ git push -f origin master
Total 0 (delta 0), reused 0 (delta 0)
To https://github.com/apexkeen/LearnGit.git
+ bed8390...674678e master -> master (forced update)
 
2000@LTDLUK3400 MINGW64 /d/GitAll/LearnGit (master|MERGING)
$ git checkout master
Readme.txt: needs merge
error: you need to resolve your current index first




Answer

You need to resolve the conflicts when git tells you they've occurred.

2000@LTDLUK3400 MINGW64 /d/GitAll/LearnGit (master)
$ git pull origin master
remote: Counting objects: 3, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
From https://github.com/apexkeen/LearnGit
* branch master -> FETCH_HEAD
8682a7a..bed8390 master -> origin/master
Auto-merging Readme.txt
CONFLICT (content): Merge conflict in Readme.txt
Automatic merge failed; fix conflicts and then commit the result.

At this point, you need to run git mergetool to fix the conflicts in the readme file. Once you have the file looking the way it should, you can run git commit and it will pre-populate the commit message with some information about the merge and the conflict. At that point, you will then be able to git push your changes to origin.

As it stands, you've then tried to do other things which is where it starts to go wrong because you must resolve the conflict first (as the command stated in its output).

Later on you've used git push with the -f flag, which resulted in the changes from the remote being lost. This would be something to avoid in a non-learning environment!

It's worth making extensive use of git status to see what is happening with your working copy at any given time.