1

How to checkout without modifying files?

Is there a git command that can do the following things?

~/target-dir (some-branch)$ mkdir /tmp/tmp-target-dir
~/target-dir (some-branch)$ cp -r * /tmp/tmp-taregt-dir
~/target-dir (some-branch)$ git checkout -f master
~/target-dir (master)$ cp -r /tmp/tmp-target-dir/* .
AnnieFromTaiwan
  • 3,845
  • 3
  • 22
  • 38
  • 1
    You can stash, check out, and then pop the stash – Rob May 04 '16 at 04:01
  • I'll have to resolve conflict if I use stash. – AnnieFromTaiwan May 04 '16 at 04:03
  • Yes, but you can always pick your version as the correct version when resolving conflicts – Rob May 04 '16 at 04:04
  • No, what I wanna perform is the exact result like above. I will then use git diff to see the overwritten result. – AnnieFromTaiwan May 04 '16 at 04:09
  • 2
    The result will be misleading, as you'll be replacing files you haven't even touched. For example, if master modified file A, but you didn't, A would show up with the inverse of the change master made if you were to run your above code. Why not just run `git diff master`? – Rob May 04 '16 at 04:14
  • Is your goal to only create new files that exist in master? Or is your goal to "be on" master with none of its changes, taking its tree? – Edward Thomson May 04 '16 at 12:19

3 Answers3

3

Use reset (--soft Does not touch the index file or the working tree at all but resets the head to <commit>):

git reset --soft master

It resets your current branch (if it is not master) not touching the files.

But seems more close to what you are asking is to move head to another branch, while keeping everything as is, just simply:

git symbolic-ref HEAD refs/heads/master

kan
  • 28,279
  • 7
  • 71
  • 101
1

I'll have to resolve conflict if I use stash

Then another approach is:

  • to create a second working tree with git worktree (git 2.5+, preferably git 2.8+):

    cd /targetdir
    git worktree add ../master master
    
  • to add the files from some-branch working tree to the master working tree

    cd ../master
    git --work-tree=../targetdir add -A .
    

That way, you can do a git diff and see what was added/modified/removed by importing some-branch working tree into master working tree.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
0

As I see from the code you wrote, you want to copy all the things from some-branch to master, so in fact you want to merge some-branch into master

~/target-dir (master)$ git merge --no-ff some-branch

This --no-ff flag make you sure that you will have the merge commit and you will be able to git diff it clearly.

If during the merge you face conflicts - you'll just need to handle them

zdolny
  • 999
  • 1
  • 11
  • 21