简体   繁体   English

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

[英]How do I add files and folders into GitHub repos?

I created an account on GitHub — I'm new on it — and I'm facing a problem with adding files.我在 GitHub 上创建了一个帐户——我是新手——我在添加文件时遇到了问题。 I have added readme.txt .我添加了readme.txt Also, I have 3 other PHP files and a folder including images.此外,我还有 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向我显示了一个错误。

You can add files using git add , example git add README , git add <folder>/* , or even git add *您可以使用git add添加文件,例如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 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示例 - 假设您创建了一个自述文件,运行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.运行git add README ,文件被暂存以进行提交。 Then run git status again, it should give you - the files have been added and ready for committing.然后再次运行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推送远程分支master

$ 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 xxx@xxx.com: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运行git 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 fetch以获取更改,然后运行git merge以合并更改。 git pull is just a combination of fetch and 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 曲线,这是一个分步指南,如果您需要一些文档和帮助

For Linux and MacOS users :对于 Linux 和 MacOS 用户:

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

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. ProTip™ :您可以仅使用 URL 预填充文件名字段。
Typing ?filename=yournewfile.txt at the end of the URL will pre-fill the filename field with the name yournewfile.txt .在 URL 末尾键入?filename=yournewfile.txt将使用名称yournewfile.txt预填充文件名字yournewfile.txt

d

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 不关心文件夹。

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)

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

When adding a directory to github check that the directory does not contain a .git file using "ls -a" if it does remove it.将目录添加到 github 时,如果确实删除了该目录,请使用“ls -a”检查该目录是否不包含 .git 文件。 .git files in a directory will cause problems when you are trying to add a that directory in git当您尝试在 git 中添加该目录时,目录中的 .git 文件会导致问题

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 ."请参阅此处的图像。”

I'm using VS SSDT on Windows.我在 Windows 上使用 VS SSDT。 I started a project and set up local version control.我启动了一个项目并设置了本地版本控制。 I later installed git and and created a Github repo.后来我安装了 git 并创建了一个 Github 存储库。 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.一旦我在 Github 上有了我的 repo,当我点击“发布到 Github”按钮时,当它要求我提供 URL 时,我抓住了 URL 并将其放入 VS。

Change directory to main folder.将目录更改为主文件夹。 Then "git add ."然后“git add”。 command will work.命令会起作用。

For me I had a folder with untracked files and subfolders the solution that saved my life was :对我来说,我有一个包含未跟踪文件和子文件夹的文件夹,拯救我生命的解决方案是:

git add --all myfolder/ git add --all 我的文件夹/

this solution makes all files and subfolders as tracked此解决方案使所有文件和子文件夹都被跟踪

I Understand where you are coming from.我明白你来自哪里。

The solution of The Drag and Drop Functionality may cease to exist.拖放功能的解决方案可能不复存在。 See below link when that happens: https://www.reddit.com/r/github/comments/meuxtg/github_drag_and_drop_not_working/发生这种情况时请参阅以下链接: https ://www.reddit.com/r/github/comments/meuxtg/github_drag_and_drop_not_working/

If somebody wants to avoid the shell and all the commands and wants to have a UI to do that, Github Desktop is one of the way to go forward.如果有人想避免使用 shell 和所有命令并希望有一个 UI 来做到这一点,那么Github Desktop是前进的方式之一。

Steps to follow to install and use Github Desktop:安装和使用 Github Desktop 的步骤:

I am assuming you know the difference between local repo and remote repo我假设您知道本地仓库和远程仓库之间的区别

  1. Install Github Desktop安装Github 桌面
  2. Create a repository locally on your hard drive by using github desktop.使用 github 桌面在硬盘上本地创建存储库。 This will automatically create files like .git and .gitattributes.这将自动创建 .git 和 .gitattributes 等文件。 It also asks to create a README.md file, always best practice is to create it and edit it informing readers about your project overview, installation steps etc. README.md is rendered in Markdown and can also render HTML.它还要求创建一个 README.md 文件,最好的做法是创建并编辑它,以告知读者您的项目概述、安装步骤等。README.md 在 Markdown 中呈现,也可以呈现 HTML。 See more about Markdown here: Markdown Cheatsheet guide在此处查看有关 Markdown 的更多信息: Markdown 备忘单指南
  3. Copy and Paste all the folders and files that you want to upload(basically the right terminology is "Push" ) into this newly created local repository.将您要上传的所有文件夹和文件(基本上正确的术语是 "Push" )复制并粘贴到这个新创建的本地存储库中。 Be aware of the directory structure as the exact same directory structure will be replicated on your remote repository.请注意目录结构,因为完全相同的目录结构将复制到您的远程存储库中。
  4. Go to github desktop, as soon as you paste files in the local repo, you will see them as changes here.转到 github 桌面,只要将文件粘贴到本地 repo 中,您就会在此处看到它们的更改。 All you need to do is commit these changes with a comment.您需要做的就是通过注释提交这些更改。 This will be your "First or Initial Commit" to the repo.这将是您对 repo 的“首次或初始提交”。
  5. Next Github repo will ask whether you want to publish these to its remote repository.下一个 Github 存储库将询问您是否要将这些发布到其远程存储库。 Click "Publish" Note Publish is just a one time operations.单击“发布”注意发布只是一次操作。 Going forward any further changes you make to local repo will be seen in github desktop and you need to again follow the loop of "Commit local->Fetch from remote->Push to Remote. As long as you are the only developer working on a project you need not go into other mechanics of git branches etc.以后您对本地存储库所做的任何进一步更改都将在 github 桌面中看到,并且您需要再次遵循“提交本地->从远程获取->推送到远程”的循环。只要您是唯一的开发人员项目你不需要进入 git 分支等的其他机制。
  6. To verify if your repo is published remotely login to your github profile on the web and see your repository sitting there.要验证您的存储库是否已远程发布,请登录到您在网络上的 github 个人资料并查看您的存储库。 This your remote repo which you effectively created from your local repo by using Github desktop.这是您使用 Github 桌面从本地存储库有效创建的远程存储库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM