You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将不同目录下的多文件添加至本地仓库并推送至远程仓库

Hey there! Let me walk you through the step-by-step process to add files from different directories to a single Git repo and push it to a remote—super straightforward once you get the hang of it.

Step 1: Initialize your Git repository (if you haven’t already)

First, pick a folder to act as your repo’s root (this can be a brand new empty folder or an existing one you want to use). Open your terminal and navigate to it:
cd /path/to/your/repo-root
Then initialize Git in this folder:
git init

Step 2: Add files from external directories

You’ve got two simple options here, depending on whether you want specific files or entire folders:

Option 1: Add individual files

Use the full file path for each item you want to include. For example:
git add /home/user/documents/reports/Q3.pdf /home/user/projects/scripts/automate.sh
This tells Git to start tracking those specific files from their original locations.

Option 2: Add entire directories

If you want to include every file inside a particular external folder, run:
git add /home/user/photos/vacation-2024/
Pro tip: Create a .gitignore file in your repo root right away to exclude any files you don’t want tracked (like temporary logs, node_modules, or sensitive .env files). Just list the paths you want ignored in that file.

Step 3: Commit your changes

Once you’ve added all the files you need, commit them with a clear, descriptive message so you (and anyone else working on the repo) knows what this commit includes:
git commit -m "Add Q3 report, automation script, and vacation photos"

First, create an empty remote repo on your platform of choice (GitHub, GitLab, Bitbucket—just don’t initialize it with a README or .gitignore if you’ve already set those up locally). Copy the remote’s URL (it’ll look like https://github.com/your-username/your-repo-name.git or an SSH URL).

Link your local repo to this remote with:
git remote add origin https://github.com/your-username/your-repo-name.git
Make sure to replace the URL with your actual remote repo address.

Step 5: Push your changes to the remote

For your first push, you’ll need to set the upstream branch (this links your local branch to the remote one):
git push -u origin main
(If your repo uses master as the default branch instead of main, just swap that word out.)

After the first push, you can just use git push for future updates to send your changes to the remote.

Quick tips to avoid common hiccups
  • If you get an error saying the remote has content you don’t (like a README created on the web), run git pull origin main --allow-unrelated-histories to merge the remote content with your local repo, then push again.
  • Always run git status before committing—it shows you exactly which files are staged to be added, so you can double-check you’re not including something by mistake.
  • If you accidentally add a file you don’t want, use git reset /path/to/unwanted/file to unstage it before committing.

内容的提问来源于stack exchange,提问作者vinay j

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 07:43:55