'Checkout a file from a remote branch and rename it [duplicate]
I would like to know if it's possible to checkout a file from a remote branch to my local branch and also rename it.
For instance:
(master) /my-project/my-awesome-file.txt
I would like to get the file by executing
git checkout master my-awesome-file.txt ---> my-file.txt
So, instead of replacing my-awesome-file.txt it would create a copy of it (from master) and name it my-file.txt
Thanks in advance.
Solution 1:[1]
Technically, you normally check out (or switch to) an entire commit. This extracts all the files from that commit. To extract a single file from a particular commit, you can limit git checkout, or use git restore, but these two do not allow you to rename the file.
What you can do quite easily is display a file from a particular commit. Simply write down the commit's name—its hash ID, or some proxy for its hash ID—followed by a colon : and the path-name of that file within that commit. Give this as an argument to git show or git cat-file -p. For instance:
git show master:my-awesome-file.txt
or:
git show a123456:path/to/file.txt
The output goes to the standard output, which you can redirect anywhere you like:
git show ${hash}:${path} > $out
where hash, path, and out are shell variables (or expand them in place with whatever you like), so:
git show master:my-awesome-file.txt > /tmp/foo.txt
If you redirect the output to a file in your current working directory, which is part of your work-tree, that file is now in your work-tree:
git show master:my-awesome-file.txt > my-file.txt
Notes about untracked files
If this file is not in Git's index (aka staging area), it is now an untracked file and will show up as such in git status unless the name of the file matches a directive in a .gitignore or similar file.
Note that the .gitignore file doesn't list files to be ignored, really. Instead, it lists files that, if untracked, Git should not complain about. If the file is tracked—that is, if its name appears in Git's index right now—the file is not ignored, even if its name is listed in .gitignore. This means that the name .gitignore is a misnomer: it should be instead .git-do-not-complain-about-these-files-if-they-are-untracked. But that doesn't cover everything .gitignore does either, so it should maybe be something even longer and less usable. But the longer and less usable this file's name gets, the more ridiculous it is, so Git just calls it .gitignore even though that's the wrong name.
Normally, git status complains about untracked files, as a reminder to you that you should use git add to make them tracked. But if the file should never be git add-ed, this complaint is worse than useless. So .gitignore lets you list files that (a) Git should not complain about, and (b) Git should skip over if you use an en-masse "add everything" operation like git add .. The skipping-over part is the main sense in which these files are really "ignored". The not-complaining is also a kind of ignoring, perhaps. But if the file is tracked—if it's gotten into Git's index somehow, perhaps by you running git add --force for instance—then it's not ignored at all.
How do you know if a file is tracked? The answer is usually "you don't". If git status doesn't complain about it, it could be because it's untracked, but it could be because you didn't change it. But in most cases, if you didn't change it, you don't care anyway, so you don't even have to ask "is this file tracked" in the first place. If git status does complain about it, saying that it is untracked, then it must be untracked, and if git status says that it's changed—and either to be committed, or not yet to be committed—then it's tracked. (Or, you can use git ls-files --staged to see everything in Git's index, but this is usually way too much information, like one of my answers here. ?)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | torek |
