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
How do I add files and folders into GitHub repos?
提问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 pull
because git push origin -u master
showed 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 README
、git add <folder>/*
甚至git add *
Then use git commit -m "<Message>"
to commit files
然后用于git commit -m "<Message>"
提交文件
Finally git push -u origin master
to push files.
最后git push -u origin master
推送文件。
When you make modifications run git status
which 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 status
gives 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 status
again, 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 master
to push the remote branch master
for the repository origin
.
最后,为存储库git push -u origin master
推送远程分支。master
origin
$ 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 master
to 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 fetch
to fetch the changes and then git merge
to merge the changes. git pull
is just a combination of fetch
and merge
.
如果您不想将上游更改与本地存储库合并,请运行git fetch
以获取更改,然后git merge
合并更改。git pull
仅仅是一个组合fetch
和merge
。
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 用户:
- First make the repository (Name=RepositoryName) on github.
- Open the terminal and make the new directory (mkdir NewDirectory).
- Copy your ProjectFolder to this NewDirectory.
- Change the present work directory to NewDirectory.
- Run these commands
- git init
- git add ProjectFolderName
- git commit -m "first commit"
- git remote add origin https://github.com/YourGithubUsername/RepositoryName.git
- git push -u origin master
- 首先在github上制作存储库(Name=RepositoryName)。
- 打开终端并创建新目录(mkdir NewDirectory)。
- 将您的 ProjectFolder 复制到这个 NewDirectory。
- 将当前工作目录更改为 NewDirectory。
- 运行这些命令
- git初始化
- git add 项目文件夹名称
- git commit -m "第一次提交"
- git 远程添加源https://github.com/YourGithubUsername/RepositoryName.git
- git push -u origin master
回答by VonC
Note that since early December 2012, you can create new files directly from GitHub:
请注意,自 2012 年 12 月上旬以来,您可以直接从 GitHub创建新文件:
ProTip?: You can pre-fill the filename field using just the URL.
Typing?filename=yournewfile.txt
at the end of the URL will pre-fill the filename field with the nameyournewfile.txt
.
专家提示?:您可以仅使用 URL 预填充文件名字段。在 URL 末尾
键入?filename=yournewfile.txt
将使用 name 预填充文件名字段yournewfile.txt
。
回答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。