36

I made a repo on my laptop. It has a project page on github.com.

I'm now working on my desktop computer. I manually copied over some files because I didn't think I would need every file from the repo (so I didn't clone the repo onto my desktop). How can I connect my desktop local folder with the existing repo so that I can push the files on my desktop to the repo (the desktop files are now the most recent versions of those files, since I stopped working from my laptop)

oiwrjgoirjgio
  • 361
  • 1
  • 3
  • 3

2 Answers2

53

Adding an existing project to GitHub using the command line:

# Initialize the local directory as a Git repository.
git init

# Add files
git add .

# Commit your changes
git commit -m "First commit"

# Add remote origin
git remote add origin <Remote repository URL>
# <Remote repository URL> looks like: https://github.com/user/repo.git

# Verifies the new remote URL
git remote -v

# Push your changes
git push origin master

And 2nd way as @evolutionxbox suggest you:

  • Clone git repo
  • Copy and paste into it
  • Push your change at origin

If in any case git reject your push you can use git push origin master --force

UPDATE (10-23-2020): Bear in mind that since October 1st, 2020, Github renamed the default repository from master to main https://github.com/github/renaming

Guillermo Garcia
  • 2,396
  • 1
  • 18
  • 23
Asif Raza
  • 3,435
  • 2
  • 27
  • 43
  • @evolutionxbox As he has an existing repo already, have chance git reject push changes and recommend the `git pull` then he will need `--force` for push their new commits as I'm thinking. – Asif Raza Apr 25 '18 at 15:41
  • 3
    Maybe `git fetch; git rebase origin/master; git push` rather than force push? Seems much safer. – dvaergiller Apr 25 '18 at 15:41
  • considering this question was asked by newbie, the order of git commands you mentioned makes git reject your push. – Nitin Nanda Jun 11 '20 at 20:49
  • @NitinNanda I have mention `--force` in case of reject. If you can improve the answer I will be happy to accept your changes. – Asif Raza Jun 11 '20 at 21:22
  • @dvaergiller commands sequence is right. But consider that from 10-01-2020, for new repositories GitHub renamed the default 'master' branch to 'main'. So, it would look like this `git fetch; git rebase origin/main; git push` – Guillermo Garcia Oct 23 '20 at 05:30
  • How can we use `git diff` to see the changes before pushing? `git pull` suggests `git branch --set-upstream-to=origin/ master` but I was not able to use it by replacing values and `git pull origin master` only shows the files changed without showing the diff. This solution does not work for me, I downloaded a ZIP file of a GiHub repository, made changes and I get an error with `git push origin master`: `! [rejected] master -> master (non-fast-forward) error: failed to push some refs` So I guess the only workaround is to clone the GitHub repository and replace the content. – baptx May 18 '21 at 16:13
1
  1. Create a local repository in the temp-dir directory using: git clone temp-dir

  2. Go into the temp-dir directory.

  3. do a git branch -a

  4. Checkout all the branches that you want to copy from origin using git checkout branch-name

You are done

Jemmy Fred
  • 11
  • 1