AnnieFromTaiwan
AnnieFromTaiwan

Reputation: 4505

Git - How to checkout without modifying files?

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/* .

Upvotes: 2

Views: 1709

Answers (3)

VonC
VonC

Reputation: 1329092

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.

Upvotes: 1

zdolny
zdolny

Reputation: 1109

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

Upvotes: 0

kan
kan

Reputation: 28981

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

Upvotes: 4

Related Questions