How to push a new code to an existing git repository in github

39,765

Solution 1

Check your remote first to see where it is pointing to by

$ git remote -v
origin  ssh://git@<old-git-url>/<project>.git (fetch)
origin  ssh://git@g<old-git-url>/<project>.git (push)

Change the pointing to GitHub

$ git remote set-url origin ssh://git@<github-url>/<project>.git

Now your repo is pointing to Github

Now you can make your changes and then add them and do a commit and finally push to remote branch; say you are on master.

git add <file>
git commit -m <commit message>
git push origin master

Solution 2

I had to do just that and achieved it like this.

Setup. I assume you have a new remote repository with files that may not cause a conflict with the directory you currently have locally.

  1. Git clone from the git repository you need to push to. Just make sure you create a new directory for the cloned code.
  2. Copy the contents of the cloned repository into the local directory that has your current code. Make sure to copy the .git (hidden) file.
  3. cd into your local directory and run git remote -v. You should see the remote repository git address.
  4. git add -A to add whatever change you require and commit it.
  5. Finally git push

Solution 3

You need to make sure that your local repository (the one that is on your computer) is connected to the remote repository (the one that is on the GitHub servers).

After this, you need to add the modified file to the staging area. Say, you have a file test.txt that you have modified, you would add it to the staging area by typing

git add test.txt

After that you would need to commit those changes. You can do that by

git commit -m "commit message"

And that's it, you have now saved those changes and recorded them in the version control. But the changes that you made have only been recorded in your local repository and you would need to push these changes to the remote repository (the GitHub servers). You can do this by

git push origin master

It would take a few seconds (depending on your internet speed and the project file size) to push these changes to the remote servers. Once it's done, you can open that repository on GitHub and see the changes for yourself.

Share:
39,765

Related videos on Youtube

Jeewantha Lahiru
Author by

Jeewantha Lahiru

I am fast learner in coding System.out.println("My name is Jeewantha");

Updated on March 03, 2021

Comments

  • Jeewantha Lahiru
    Jeewantha Lahiru about 3 years

    I need to push my modified new java code to my old git repository in github but I do not have old code in my pc. How to do that?

    I had push a code before my github account before. Now I don't have that old code in my pc. How do I pull the project into my pc and after making changes, push again to the same repository?

    I do not have much experience in github, so please help me to improve skills on github.

    • Green Cloak Guy
      Green Cloak Guy over 4 years
      Can you provide more information, such as your operating system and the process you used to download the git repository?
    • Jeewantha Lahiru
      Jeewantha Lahiru over 4 years
      my operating system is Windows and I have a repository in github that I have created before.now I have developed my old code and I need to Push this code to that old repository. @GreenCloakGuy
    • Goran Vasic
      Goran Vasic over 4 years
      You should add your old repository as a new origin, e.g. git remote add old-repo <old-repo-url> Then you can push the code like this: git push old-repo <branch-name>