Git操作咨询:本地文件恢复及从自有Git服务器拉取文件
Hey there! Let's tackle your two Git questions since you've already got the push/pull flow between your local machine and dev server working—nice work on that setup!
1. Recovering Deleted Local Files & Fixing git checkout Errors
First, let's break down how to get your deleted files back, then figure out why git checkout is throwing exceptions at you:
Case 1: The file was tracked by Git, and you haven't committed the deletion
If you just deleted the file (either via terminal rm or your file explorer) and haven't run git commit yet, you can restore it directly from Git's index with this command:
git checkout -- path/to/your/deleted/file
Pro tip: Make sure you use the exact relative path to the file. If you get an error here, run git status—it'll show the deleted file with its full path (look for the deleted: line) so you can copy-paste that instead of typing it out.
Case 2: You already committed the deletion
If the deletion is part of a recent commit, you can pull the file from the commit right before the deletion. First, find the hash of the commit that deleted the file using git log (look for the commit message that mentions deleting the file). Then run:
git checkout [commit-hash]~1 -- path/to/your/deleted/file
The ~1 tells Git to grab the version of the file from the parent commit (the state before the deletion happened).
Why git checkout might be failing
- Incorrect file path: If Git says "pathspec did not match any file(s) known to Git", you almost certainly typed the path wrong. Use
git statusto get the exact path as mentioned above. - File was never tracked: If you never ran
git addon the file (so Git doesn't know it exists), Git can't restore it. You'll need to use your operating system's file recovery tools for this scenario. - Branch confusion: If you're trying to pull the file from a different branch, make sure you specify the branch name explicitly, like
git checkout main -- path/to/file.
2. Letting Other Developers Pull from Your Self-Hosted Git Server
Getting your team set up to pull from your own server is pretty straightforward—here's what they need to do:
Share your server's repo URL
Your self-hosted repo will have either an SSH or HTTPS URL. For example, an SSH URL might look likegit@your-server-ip:/path/to/your/repo.gitorssh://your-user@your-server-ip:/var/git/repo.git.Make sure they have access
- SSH access: They'll need to send you their public SSH key (usually found in
~/.ssh/id_rsa.pubon their machine). You'll add this key to the~/.ssh/authorized_keysfile of the user that owns the repo on your server—this lets them authenticate without entering a password every time. - HTTPS access: If you're using HTTPS, they'll need the username and password for the server (or a personal access token if your server supports it) when they first clone or pull.
- SSH access: They'll need to send you their public SSH key (usually found in
Clone the repo locally
Once they have the URL and access, they can create a local copy of the repo with:git clone <your-server-repo-url>Pulling updates later
After cloning, whenever they want to get the latest changes from your server, they just run:git pull origin <branch-name>Replace
<branch-name>with your main branch (likemainormaster).
Bonus: If they already have a local repo
If a developer is working from an existing local repo and wants to add your server as an additional remote, they can run:
git remote add upstream <your-server-repo-url>
Then pull updates with:
git pull upstream <branch-name>
内容的提问来源于stack exchange,提问作者An0n

