How do I add files and folders into GitHub repos?

GitGithub

Git Problem Overview


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.

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 Solutions


Solution 1 - Git

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

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

Finally git push -u origin master to push files.

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

Example - say you created a file README, running git status gives you

$ 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 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'
[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
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

$ 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.

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

Solution 2 - Git

For Linux and MacOS users :

  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

Solution 3 - Git

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

Create new File

> 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 name yournewfile.txt.

d

Solution 4 - Git

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

This is because git does not care about folders.

Solution 5 - 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.

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)

Solution 6 - Git

> 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

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

after that

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

Solution 7 - Git

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

Solution 8 - Git

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

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

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.

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

Solution 9 - Git

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.

Solution 10 - Git

Change directory to main folder. Then "git add ." command will work.

Solution 11 - Git

For me I had a folder with untracked files and subfolders the solution that saved my life was :

git add --all myfolder/

this solution makes all files and subfolders as tracked

Solution 12 - Git

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/

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.

Steps to follow to install and use Github Desktop:

I am assuming you know the difference between local repo and remote repo

  1. Install Github Desktop
  2. Create a repository locally on your hard drive by using github desktop. This will automatically create files like .git and .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. See more about Markdown here: Markdown Cheatsheet guide
  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. 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. All you need to do is commit these changes with a comment. This will be your "First or Initial Commit" to the repo.
  5. Next Github repo will ask whether you want to publish these to its remote repository. 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.
  6. To verify if your repo is published remotely login to your github profile on the web and see your repository sitting there. This your remote repo which you effectively created from your local repo by using Github desktop.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAdnanView Question on Stackoverflow
Solution 1 - GitFirst ZeroView Answer on Stackoverflow
Solution 2 - GitAnkit BhatiaView Answer on Stackoverflow
Solution 3 - GitVonCView Answer on Stackoverflow
Solution 4 - GitGautierView Answer on Stackoverflow
Solution 5 - GitGeviousView Answer on Stackoverflow
Solution 6 - GitShashwat GuptaView Answer on Stackoverflow
Solution 7 - GitMattView Answer on Stackoverflow
Solution 8 - GitRavenReemaView Answer on Stackoverflow
Solution 9 - GitcromastroView Answer on Stackoverflow
Solution 10 - GitNodirbek ErgashevView Answer on Stackoverflow
Solution 11 - GitELHILI AbdelmounaimView Answer on Stackoverflow
Solution 12 - GitAchal KagwadView Answer on Stackoverflow