简体   繁体   中英

git checkout file from branch ahead of current branch

I've created a local git repo, which contains 2 branches ie master and stable
Using master as a development branch and it has gone 4-5 commits ahead of stable and it also contains some unstaged edits.

The problem is that a bug has come in against stable , so I've stashed my unstaged edits on master and performed a checkout against stable . Now I need a file from master , specifically travis.yml , so I can add it to stable for when I commit the bug fix.

Is there a clean way to do this so that when I merge stable back into master there are no conflicts with the existing travis.yml ?

This is what I've tried but no luck ..

rob@laptop:~/git/project$ git status
On branch stable
Your branch is up-to-date with 'origin/stable'.

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

    modified:   config/user.properties

no changes added to commit (use "git add" and/or "git commit -a")

rob@laptop:~/git/project$ git checkout master/86e6e26acf .travis.yml
error: pathspec 'master/86e6e26acf' did not match any file(s) known to git.
error: pathspec '.travis.yml' did not match any file(s) known to git.

So after bug fix are you expecting "travis.yml" to be same across master and stable? In that case easy way would be checkout the master, copy the contents of travis.yaml into a temp location, checkout stable replace travis.yaml in stable with the one you had saved.

Git is intelligent enough to identify that both the files are same and not to show any merge conflicts. Infact git will only store a single file for both the branches. I hope this helps.

@R0MANARMY answer worked for me ..

"Error seems pretty self explanatory, pathspec master/... doesn't exist. I'm pretty sure the command you're looking for is git checkout master -- .travis.yml . There's a related question here: Meaning of Git checkout double dashes. "

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM