Git 中文件检出与重命名

保存时间:2026/4/3 01:35:34


Git 中文件检出与重命名
内容由豆包 AI 生成
how to checkout source file and output in a different name in git
In Git, if you want to retrieve a source file and save it with a different name, you can utilize the git show command. Here is a detailed guide:

1. Check out a file from the current branch

If you wish to get a file from the current branch and rename it, you can use the following command:
bash
运行
git show HEAD:path/to/source.file > new-filename.file
  • HEAD indicates the latest commit on the current branch.
  • path/to/source.file is the original file path in the repository.
  • new-filename.file is the new name you assign to the file.

2. Check out a file from a specific commit

When you need to retrieve a file from a particular historical commit and give it a new name, use this command:
bash
运行
git show commit-hash:path/to/source.file > new-filename.file
  • commit-hash represents the hash value of the target commit.

3. Check out a file from another branch

To obtain a file from a different branch and rename it, the command is as follows:
bash
运行
git show branch-name:path/to/source.file > new-filename.file
  • branch-name is the name of the branch containing the file you want.

Example

Suppose there is a file named old.txt in the repository, and you want to check it out and name it new.txt, you can run:
bash
运行
git show HEAD:old.txt > new.txt

Notes

  • The git show command will not change the files in your working directory or the repository history.
  • The newly named file will be an untracked file in the repository. If you want Git to track it, you need to execute git add new-filename.file.
How to checkout a specific version of a file and output it with a different name?
Can I use other commands to achieve the same function?
What are the common use cases for renaming files retrieved from Git?