0

I am royally confused with what git checkout [<tree-ish>] [--] <pathspec>…​.. does

from git documentation

My repo. contains folders and I have an old branch lets call it old_branch. I want to merge one folder from the old_branch to my dev

I switched to my dev like so:

git checkout dev 

then I wanted to merge one folder from my old branch to dev

git checkout <old_branch> --myfolder 

Question: Does it do a merge with my dev , or simply replaces code from my old_branch and marks it as "merged"

since when I tried doing git checkout -b new_branch, committing and then doing git merge dev , it told me that there were no changes (up to date).

When examining the code I see lots of deletes that should not have happened.

Update 1 I want to merge (combine changes) the content of one of subfolders in my current tree (dev) with the content of one of my subfolders in the tree (old_branch)

Update 2 Looks like I asked a wrong question. The real question should be is how to merge a subfolder in a mono-repo set up . I don't think there is an automated way to do this. I ended up doing it by hand.

6
  • This operation has nothing to do with a merge. What do you call "mark it as merged" ? Where ? Commented Jul 9, 2019 at 21:28
  • @EomainValeri After the checkout , I do a merge command, i get a message saying that the branch is "up to date". That is obviously not true since the code is very different. Commented Jul 9, 2019 at 21:53
  • Having the same code and being merged adress completely different things. You can have branches merged but having very different code, and you can also have branches unmerged although their code is 100% identical. (I'm aware that my remarks aren't helpful at all, since you're asking about this very point. But I think this is a misunderstanding on core concepts, not on a technicality.) Commented Jul 9, 2019 at 22:06
  • It is possible to have git checkout attempt to merge files, using git checkout -m. Don't go there :-) ... at least not now, because that really will muddy the waters. As long as you're not using git checkout -m (which gets complicated), see Ross Hunter's answer. Commented Jul 9, 2019 at 23:08
  • i think i want that feature, what is confusing to me is the fact that i am dealing with a subfolder, not the whole repo. Commented Jul 10, 2019 at 0:22

1 Answer 1

3

Does it do a merge with my dev , or simply replaces code from my old_branch and marks it as "merged"

git checkout does neither of those things.

I wanted to merge one folder from my old branch to dev

You're on the right track, but there's some confusion about syntax. git merge is usually used to combine the changes from two or more commits into a single, new commit (unless it's a fast-forward, but that's not important here). If I understand correctly, you want to replace one version of a folder that exists on one commit (i.e. the latest commit on branch dev) with a version of the same folder from a different commit (i.e. on old_branch). Here's the command sequence you need to do that, with explanations:

  1. First, make sure we're on branch dev with git checkout dev
  2. Replace the version of myfolder in the staging area and work area with the version from old_branch: git checkout old_branch -- myfolder. Note this does NOT modify the committed version that's in your repository. In git-speak, we say that HEAD still points to the same commit.
  3. Now we need to "save" the change we made to myfolder in our work area by committing it to branch dev. Because checkout automatically adds checked-out files/folders to the staging area, we only need to commit the changes:

    git commit -m 'change myfolder to version on branch old_branch'

  4. You can see your new commit on branch dev by running git log. The top commit should have the commit message above.

[Edit 1]: @JavaHead This post might be of interest to you. If myfolder on old_branch has files that don't exist in the same folder on dev, and you want them to be added to dev, just checkout the files you want from old_branch into myfolder on dev and commit, just like in the flow above. If you mean that each branch has different versions of the same files, then you probably need git checkout -p old_branch -- <file-to-combine>. This will show you a diff of the changes to the file in each branch, and let you pick which changes to make on an individual basis.

Sign up to request clarification or add additional context in comments.

3 Comments

Minor but sometimes important: in step 2, git checkout old-branch -- myfolder actually does the checkout through the index / staging area. That means that the updates are staged, and step 3 does not require a separate git add (but it won't hurt either).
I want to merge (combine changes ) subfolder in my current directory on the latest branch (dev) with content of one of the folders in old_branch. Please forgive me if i did not describe it clearly.
@torek Ahh right you are, thanks! I've updated my answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.