git

git Study Log

Posted by JoyLee on May 22, 2025

1. How to save a specific file from a historical commit in the local repository to the current state?

Method: Use the git checkout command

  1. Find the hash of the target commit
  • Use the git log command to view the commit history and locate the hash of the target commit. For example:

    1
    
    git log
    

    Assume the hash of the target commit is abc123.

  1. Check out the target file from the historical commit to the current working directory
  • Use the git checkout command, specifying the hash of the target commit and the file path. For example:

    1
    
    git checkout abc123 -- path/to/your/file
    

    This will check out the file path/to/your/file from the target commit into the corresponding location in the current working directory.

2. Using Git Worktree

Use Cases

Use Git Worktree to create multiple working directories. In different working directories, you can switch to different branches, making it convenient to compare code from different branches simultaneously without switching in the same working directory.

💡 A working directory is like a display screen. Opening multiple working directories is equivalent to opening multiple screens, which can improve work efficiency.

Basic Operations

Here’s a detailed guide on how to create a new workspace with Git. Git doesn’t have a built-in “workspace” command, but typically refers to the following scenarios:


Git Worktree allows you to create multiple independent working directories within the same repository, where each directory can checkout different branches without interfering with each other.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 1. Navigate to your project root directory
cd /path/to/your/project

# 2. Create a new working space (based on an existing branch)
git worktree add ../project-feature-branch feature-branch-name

# Or create a new branch and establish a working space based on the current branch
git worktree add -b new-feature ../project-new-feature

# 3. View all working spaces
git worktree list

# 4. Remove a working space
git worktree remove ../project-feature-branch

Characteristics:

  • Shares the same .git repository, saving disk space
  • Different working spaces can be on different branches simultaneously
  • Avoids the hassle of frequent git stash branch switching

Method 2: Clone to a New Directory (Traditional Approach)

1
2
3
4
5
# Clone to a brand new independent directory
git clone /path/to/original/project /path/to/new/workspace

# Or clone from remote
git clone https://github.com/user/repo.git ./new-workspace

Characteristics:

  • Completely independent copies
  • Consumes double disk space
  • Suitable for long-term independent development lines

Quick Comparison

Method Use Case Disk Usage Branch Switching
git worktree Developing multiple features simultaneously Low (shared repository) No switching needed, parallel work
git clone Completely isolated environments High (complete copy) Independently managed

Practical Recommendations

If you frequently need to switch between multiple branches for development, git worktree is the best choice.