13

Suppose I have 2 branches, master and other.

I go in the other branch, add 2 files, commit and push.

Now I go into the master branch, add files to a different directory, and commit them. Then I merge other.

The problem is that the files I added in other are not showing up. Git says it is up-to-date, but its not! Files are missing.

How can I force master to add the files in other or somehow manually add them?

Edit for Karl:

I did the following to the best of my knowledge, although the changes that are not showing up are several weeks old. I just realized they weren't there today.

$ git branch
*other
master
$ git add .
$ git commit -m 'cool new features'
$ git push origin other
$ git checkout master
$ git merge other
$ git add .
$ git commit -m 'merged cool new features from other'
$ git push origin master

I go on Github, and the files aren't there. Other files were committed and show up, but two folders do not have matching content. The files exist in other but not in master. To clarify, the files were not new. But I thought that merging would at least copy files to master if they dont exist!

AJcodez
  • 31,780
  • 20
  • 84
  • 118
  • Hi, are you sure your files have been commited? There's no reason Git would hide them away during the merge. Can you post all the commands you've made? (This will help find out the problem) – Simon Boudrias Nov 29 '12 at 03:29
  • git add ., git commit -m 'something', git push, git checkout, git merge. Probably in every order by now and many times – AJcodez Nov 29 '12 at 03:38
  • And what happen if you do `git status` in `other` branch ? – Simon Boudrias Nov 29 '12 at 03:53
  • In branch `master`, execute `git branch --merged`, if it's not listing the `other` branch, then the `other` branch is not merged with `master`. – Karthik Bose Nov 29 '12 at 04:09
  • `git branch --merged` shows *other as expected. `git status` gives nothing to commit (working directory clean) – AJcodez Nov 29 '12 at 06:05
  • 1
    I think [this SO post](http://stackoverflow.com/questions/3472804/git-merge-incomplete-missing-files-and-folders) has one of reasons why it happened. – user3290525 Mar 13 '17 at 06:58

4 Answers4

14

It's a little late, but for other users who find this question, I will describe a situation where the problem observed by AJcodez will occur. If you do a git checkout master; git merge other, during which or after which you delete some of the new files in master (and possibly forget about that fact) and later do a git checkout master; git merge other again, then the "new" files will not reappear in master, because they are not new. Merge only cares about changes relative to the merge-base, which is the youngest commit reachable via both branches. During your second merge, the merge-base is not the same as during your first merge, the merge-base during the second merge in the described scenario is the commit that was the tip of other during the first merge. If other didn't change the new files since then, then the deletion in master is the most current change and therefore deletion of the new files will be the state after the second merge.

If that seems inconvenient, you can get around it by creating a temporary branch (let's call it merge_branch), merging other into it using --squash, committing, merging it into master and deleting the temporary branch. It's probably not the ideal solution (e.g. already resolved merge conflicts might have to be resolved again), but then again the original situation is probably already the result of a mistake. Here is what I mean in code:

git log other # find the commit where 'other' originally branched off
git checkout -b merge_branch COMMIT_ID # go there & create branch
# without '--squash', the following merge would simply be a fast-forward
# merge and wouldn't solve our problem, because 'merge_branch' would then
# point to the same commit as 'other' and so the later merge into
# 'master' would produce the same unsatisfactory result.
git merge --squash other # does not create a new commit by itself
git commit # better add an explanation for what you did and why
# 'merge_branch' now contains everything you did in 'other' since it
# branched off from 'master', but squashed into a single new commit
git checkout master
git merge merge_branch
git branch --delete merge_branch
user2845840
  • 360
  • 3
  • 9
  • Bro, you are soooo professional and deep into how git works! Saved my day, I still cannot fully comprehend what you are saying, only see that it is true, and that this is such a hard problem to catch and then solve!!! – pinpinokio Jan 21 '21 at 19:05
  • 1
    You're welcome. If you want to know more, I recommend reading the git-merge-manpage. I myself couldn't understand the problem until I read that manpage and experimented a lot. – user2845840 Jan 22 '21 at 20:29
  • Maybe a bit late to the party but a quick way to resolve is simply: 1) delete branch 2) checkout from server. You will get those files back – Eli Fry Jan 31 '23 at 16:05
4

Like this:

karl@Bielefeldt-Server:~/stackoverflow$ git init .
Initialized empty Git repository in /home/karl/stackoverflow/.git/
karl@Bielefeldt-Server:~/stackoverflow$ touch common_file_a
karl@Bielefeldt-Server:~/stackoverflow$ touch common_file_b
karl@Bielefeldt-Server:~/stackoverflow$ git add .
karl@Bielefeldt-Server:~/stackoverflow$ git commit -m "commit common files"
[master (root-commit) 89a5cd0] commit common files
 0 files changed
 create mode 100644 common_file_a
 create mode 100644 common_file_b
karl@Bielefeldt-Server:~/stackoverflow$ git checkout -b other
Switched to a new branch 'other'
karl@Bielefeldt-Server:~/stackoverflow$ mkdir other
karl@Bielefeldt-Server:~/stackoverflow$ touch other/other_file_a
karl@Bielefeldt-Server:~/stackoverflow$ touch other/other_file_b
karl@Bielefeldt-Server:~/stackoverflow$ git add .
karl@Bielefeldt-Server:~/stackoverflow$ git commit -m "commit other files"
[other 9c7409c] commit other files
 0 files changed
 create mode 100644 other/other_file_a
 create mode 100644 other/other_file_b
karl@Bielefeldt-Server:~/stackoverflow$ git checkout master
Switched to branch 'master'
karl@Bielefeldt-Server:~/stackoverflow$ touch master_file_a
karl@Bielefeldt-Server:~/stackoverflow$ touch master_file_b
karl@Bielefeldt-Server:~/stackoverflow$ git add .
karl@Bielefeldt-Server:~/stackoverflow$ git commit -m "commit master files"
[master 3558768] commit master files
 0 files changed
 create mode 100644 master_file_a
 create mode 100644 master_file_b
karl@Bielefeldt-Server:~/stackoverflow$ ls
common_file_a  common_file_b  master_file_a  master_file_b
karl@Bielefeldt-Server:~/stackoverflow$ git merge other
Merge made by the 'recursive' strategy.
 0 files changed
 create mode 100644 other/other_file_a
 create mode 100644 other/other_file_b
karl@Bielefeldt-Server:~/stackoverflow$ ls
common_file_a  common_file_b  master_file_a  master_file_b  other
karl@Bielefeldt-Server:~/stackoverflow$ ls other
other_file_a  other_file_b

If you're getting different results, you're either missing a step, putting an extra step somewhere, or you're getting some sort of error you're not telling us about, like a merge conflict. We have no way of knowing why something so basic isn't working for you unless you post the exact commands and output you're getting, like I did above.

Karl Bielefeldt
  • 47,314
  • 10
  • 60
  • 94
1

I solved this problem by creating empty file with the same name at master branch:

Suppose, the branch other contains a new file newfile.txt that was not merged somehow to master.

git checkout master
touch newfile.txt
git add newfile.txt
git commit -m "create newfile.txt"
git merge other

It is kind of dirty, but works.

bilbohhh
  • 693
  • 1
  • 6
  • 16
1

Happend to me that only doing a

git merge other

is not enough. I had to pull changes from the other branch before merging with

git checkout other
git pull
git checkout first

and then I was able to

git merge other
Pipo
  • 4,653
  • 38
  • 47