0

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

2
  • 2
    Can you give the actual git command lines and git output instead?
    – AnoE
    Commented Aug 16, 2016 at 15:09
  • One of the things that seems to be a great hurdle for beginners is that your analysis of a situation is often incorrect. In fact, it is usually how you got into whatever mess you are in in the first place. This is why we insist on seeing the actual source code or commands you ran for virtually all questions, and especially ones by beginners. Commented Aug 16, 2016 at 15:12

2 Answers 2

1

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.

1
  • ah .. important thing I missed is. .after merge, i forgot to do git commit.. which was creating all sorts of problem. Though I've still few things unanswered ..but still would mark this answer .. thank you ! Commented Aug 17, 2016 at 11:06
1
  1. Commit your code in test branch.
  2. Checkout to master and do git pull origin master, make sure your master is up to date with repository.
  3. Checkout to test branch and do the rebasing.
  4. git rebase master, if its done without any conflict than you are good to go otherwise manually solve the conflicts in the particular file.
  5. after solving the conflicts do git rebase --continue
  6. after that do git add . & git commit.
  7. you are all set now you will have all the changes from master and test branch.
4
  • where does number 6 happen? When you rebase there's no need for a commit?
    – rubenvb
    Commented Aug 16, 2016 at 15:30
  • Then you typically do git add . and git rebase --continue. No need or want for a git commit in that flow, anywhere...
    – rubenvb
    Commented Aug 17, 2016 at 6:09
  • Introducing rebase at this stage is only going to complicate things for the OP
    – bcmcfc
    Commented Aug 17, 2016 at 7:55
  • Thanks for your Reply Ghayyour and all.. But rebasing n all is still very confusing for me at this stage and I know I can do merge without going into that direction.. but thanks again ! Commented Aug 17, 2016 at 11:07

Not the answer you're looking for? Browse other questions tagged or ask your own question.