git 如何将文件和文件夹添加到 GitHub 存储库中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8775850/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 12:40:31  来源:igfitidea点击:

How do I add files and folders into GitHub repos?

gitgithub

提问by Adnan

I created an account on GitHub — I'm new on it — and I'm facing a problem with adding files. I have added readme.txt. Also, I have 3 other PHP files and a folder including images.

我在 GitHub 上创建了一个帐户——我是新用户——但我在添加文件时遇到了问题。我已经添加了readme.txt。另外,我还有 3 个其他 PHP 文件和一个包含图像的文件夹。

How do I add the files and folder? I tried it with git pullbecause git push origin -u mastershowed me an error.

如何添加文件和文件夹?我尝试了它,git pull因为git push origin -u master向我显示了一个错误。

回答by First Zero

You can add files using git add, example git add README, git add <folder>/*, or even git add *

您可以使用git add、 example git add READMEgit add <folder>/*甚至git add *

Then use git commit -m "<Message>"to commit files

然后用于git commit -m "<Message>"提交文件

Finally git push -u origin masterto push files.

最后git push -u origin master推送文件。

When you make modifications run git statuswhich gives you the list of files modified, add them using git add *for everything or you can specify each file individually, then git commit -m <message>and finally, git push -u origin master

当您进行修改运行时git status,它会为您提供修改的文件列表,将它们添加到git add *所有内容中,或者您可以单独指定每个文件,然后git commit -m <message>最后,git push -u origin master

Example - say you created a file README, running git statusgives you

示例 - 假设您创建了一个 README 文件,运行git status会为您提供

$ git status
# On branch master
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#   README

Run git add README, the files are staged for committing. Then run git statusagain, it should give you - the files have been added and ready for committing.

运行git add README,文件被暂存以进行提交。然后git status再次运行,它应该给你 - 文件已经添加并准备好提交。

$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#   new file:   README
#

nothing added to commit but untracked files present (use "git add" to track)

Then run git commit -m 'Added README'

然后运行 git commit -m 'Added README'

$ git commit -m 'Added README'
[master 6402a2e] Added README
  0 files changed, 0 insertions(+), 0 deletions(-)
  create mode 100644 README

Finally, git push -u origin masterto push the remote branch masterfor the repository origin.

最后,为存储库git push -u origin master推送远程分支。masterorigin

$ git push -u origin master
Counting objects: 4, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 267 bytes, done.
Total 3 (delta 1), reused 0 (delta 0)
To [email protected]:xxx/xxx.git
   292c57a..6402a2e  master -> master
Branch master set up to track remote branch master from origin.

The files have been pushed successfully to the remote repository.

文件已成功推送到远程存储库。

Running a git pull origin masterto ensure you have absorbed any upstream changes

运行 agit pull origin master以确保您吸收了任何上游更改

$ git pull origin master
remote: Counting objects: 12, done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 8 (delta 4), reused 7 (delta 3)
Unpacking objects: 100% (8/8), done.
From xxx.com:xxx/xxx
 * branch            master     -> FETCH_HEAD
Updating e0ef362..6402a2e
Fast-forward
 public/javascript/xxx.js |    5 ++---
 1 files changed, 2 insertions(+), 3 deletions(-)
 create mode 100644 README

If you do not want to merge the upstream changes with your local repository, run git fetchto fetch the changes and then git mergeto merge the changes. git pullis just a combination of fetchand merge.

如果您不想将上游更改与本地存储库合并,请运行git fetch以获取更改,然后git merge合并更改。git pull仅仅是一个组合fetchmerge

I have personally used gitimmersion - http://gitimmersion.com/to get upto curve on git, its a step-by-step guide, if you need some documentation and help

我个人使用 gitimmersion - http://gitimmersion.com/来了解 git 曲线,这是一个分步指南,如果您需要一些文档和帮助

回答by Ankit Bhatia

For Linux and MacOS users :

对于 Linux 和 MacOS 用户:

  1. First make the repository (Name=RepositoryName) on github.
  2. Open the terminal and make the new directory (mkdir NewDirectory).
  3. Copy your ProjectFolder to this NewDirectory.
  4. Change the present work directory to NewDirectory.
  5. Run these commands
    1. git init
    2. git add ProjectFolderName
    3. git commit -m "first commit"
    4. git remote add origin https://github.com/YourGithubUsername/RepositoryName.git
    5. git push -u origin master
  1. 首先在github上制作存储库(Name=RepositoryName)。
  2. 打开终端并创建新目录(mkdir NewDirectory)。
  3. 将您的 ProjectFolder 复制到这个 NewDirectory。
  4. 将当前工作目录更改为 NewDirectory。
  5. 运行这些命令
    1. git初始化
    2. git add 项目文件夹名称
    3. git commit -m "第一次提交"
    4. git 远程添加源https://github.com/YourGithubUsername/RepositoryName.git
    5. git push -u origin master

回答by VonC

Note that since early December 2012, you can create new files directly from GitHub:

请注意,自 2012 年 12 月上旬以来,您可以直接从 GitHub创建新文件

Create new File

创建新文件

ProTip?: You can pre-fill the filename field using just the URL.
Typing ?filename=yournewfile.txtat the end of the URL will pre-fill the filename field with the name yournewfile.txt.

专家提示?:您可以仅使用 URL 预填充文件名字段。在 URL 末尾
键入?filename=yournewfile.txt将使用 name 预填充文件名字段yournewfile.txt

d

d

回答by Gautier

If you want to add an empty folder you can add a '.keep' file in your folder.

如果您想添加一个空文件夹,您可以在您的文件夹中添加一个“.keep”文件。

This is because git does not care about folders.

这是因为 git 不关心文件夹。

回答by Gevious

You need to checkout the repository onto your local machine. Then you can change that folder on your local machine.

您需要将存储库签出到本地计算机上。然后您可以在本地计算机上更改该文件夹。

git commit -am "added files"

That command will commit all files to the repo.

该命令会将所有文件提交到 repo。

git push origin master

that will push all changes in your master branch (which I assume is the one you're using) to the remote repository origin (in this case github)

这会将您的主分支(我假设是您正在使用的分支)中的所有更改推送到远程存储库源(在本例中为 github)

回答by Shashwat Gupta

Simple solution:

简单的解决方案:

git init
git add =A
git commit -m "your commit"
git push -u origin master

if you want add folder to existing repo ..then add folder to local project code

如果要将文件夹添加到现有 repo ..then 将文件夹添加到本地项目代码

git rm --cached ./folderName
 git add ./folderName

after that

在那之后

git status
git commit -m "your commit"
git push -u origin master

回答by Matt

When adding a directory to github check that the directory does not contain a .git file using "ls -a" if it does remove it. .git files in a directory will cause problems when you are trying to add a that directory in git

将目录添加到 github 时,如果确实删除了该目录,请使用“ls -a”检查该目录是否不包含 .git 文件。当您尝试在 git 中添加该目录时,目录中的 .git 文件会导致问题

回答by RavenReema

Check my answer here : https://stackoverflow.com/a/50039345/2647919

在这里检查我的答案:https: //stackoverflow.com/a/50039345/2647919

"OR, even better just the ol' "drag and drop" the folder, onto your repository opened in git browser.

“或者,更好的是将 ol”“拖放”文件夹到在 git 浏览器中打开的存储库中。

Open your repository in the web portal , you will see the listing of all your files. If you have just recently created the repo, and initiated with a README, you will only see the README listing.

在门户网站中打开您的存储库,您将看到所有文件的列表。如果您最近刚刚创建了 repo,并使用 README 启动,您将只会看到 README 列表。

Open your folder which you want to upload. drag and drop on the listing in browser. See the image here."

打开您要上传的文件夹。拖放浏览器中的列表。请参阅此处的图像。”

回答by cromastro

I'm using VS SSDT on Windows. I started a project and set up local version control. I later installed git and and created a Github repo. Once I had my repo on Github I grabbed the URL and put that into VS when it asked me for the URL when I hit the "publish to Github" button.

我在 Windows 上使用 VS SSDT。我启动了一个项目并设置了本地版本控制。后来我安装了 git 并创建了一个 Github 存储库。一旦我在 Github 上有了我的 repo,当我点击“发布到 Github”按钮时,当它要求我提供 URL 时,我抓住了 URL 并将其放入 VS。