Hello @sphericalsilver,

You want to update the master branch, correct? If so, you should be able to run  git push origin master to push your local changes to your online repository. The following page has more info on git push.

https://help.github.com/en/articles/pushing-commits-to-a-remote-repository

Another alternative is to use GitHub Desktop. With this you can clone your repository to your local system, make changes, and then commit and push these changes to your online repository. This the method I use to update my GitHub pages.

🌐
GitHub
docs.github.com › en › get-started › using-git › pushing-commits-to-a-remote-repository
Pushing commits to a remote repository - GitHub Docs
When you clone a repository you own, you provide it with a remote URL that tells Git where to fetch and push updates. If you want to collaborate with the original repository, you'd add a new remote URL, typically called upstream, to your local Git clone: ... git fetch upstream # Grab the upstream remote's branches > remote: Counting objects: 75, done. > remote: Compressing objects: 100% (53/53), done. > remote: Total 62 (delta 27), reused 44 (delta 9) > Unpacking objects: 100% (62/62), done. > From https://github.com/OCTOCAT/REPO > * [new branch] main -> upstream/main
🌐
GitHub
github.com › orgs › community › discussions › 68237
Pushing Code into a github Repo · community · Discussion #68237
6. Add the Remote Repository Link your local repository to the GitHub repository you created. Replace USERNAME with your GitHub username and REPOSITORY_NAME with the name of your GitHub repository: git remote add origin https://github.com/USERNAME/REPOSITORY_NAME.git · 7. Push Your Code Push your changes to GitHub.
Discussions

Pushing to GitHub via the terminal
Here's where to start reading about git: https://www.git-scm.com/doc . From the Pro Git book to the reference manual, it's the site that will be your best git friend from the first day and through the most complex problems and advanced usage. More on reddit.com
🌐 r/git
15
0
March 31, 2023
How do you push changes to your GitHub page without deleting everything and re-uploading it?
How do you push changes to your GitHub page without deleting everything and re-uploading it? More on github.com
🌐 github.com
2
6
August 11, 2019
How do I push my local git repository to a branch in a remote github repository? - Stack Overflow
My organization has prevented pushing to master. I instead need to create a separate branch on GitHub explicitly and push my code to that branch and then create a pull request. I have my code in a ... More on stackoverflow.com
🌐 stackoverflow.com
Unable to push a repository from Git to Github
Thank you everyone for your help! Through Stack overflow, I have learned there seem to be some sort of bug. The version of Git is fairly new (10 days old) It is not fixed, but managed to copy the same GitHub code in GitHub desktop "Open in command prompt" and it worked. As part of my course I was supposed to do this in this particular way (Git bash, Github, push, HTTPS). But in the end of the day the point of this if for me to be able to push my project to github. Through my rather obsessive attitude towards (just wanted to know why it didn't work soooo baaaad!!!) I have learned about : listing my git config fetch and push the story behind replacing master with main git remote -version practice git workflow (since I have to start the whole thing a couple of time) understanding GitHub push code (azzal07) Reaching to the community instead of relying on my tutor Googling (the sh*t out of it) for information Different type of credential Personal access token and so on ... So it wasn't a waste of time. It is all part of the beauty of learning isn't it ? after every mistake, every struggle I am learning something ! "If there is a will, there is a way" More on reddit.com
🌐 r/learnprogramming
10
1
June 19, 2021
🌐
Microsoft Developer Blogs
devblogs.microsoft.com › dev blogs › azure devops blog › optimizing git policy management at scale
Optimizing Git policy management at scale - Azure DevOps Blog
1 week ago - For these reasons, we and many other big enterprises rely on the REST API in Azure DevOps and GitHub to audit and mitigate policy misconfiguration and drift across the entire portfolio of repos. A dedicated service defines the target state of policies and automatically updates them when the actual state drifts from the target. Let me walk you through how the policies are stored and retrieved. There are two types of Git policies in Azure Repos: push and branch policies.
🌐
Git
git-scm.com › docs › git-push
Git - git-push Documentation
The "remote" repository that is the destination of a push operation. This parameter can be either a URL (see the section GIT URLS below) or the name of a remote (see the section REMOTES below). ... Specify what destination ref to update with what source object.
🌐
GitHub
docs.github.com › en › migrations › importing-source-code › using-the-command-line-to-import-source-code › adding-locally-hosted-code-to-github
Adding locally hosted code to GitHub - GitHub Docs
For example, gh repo create --source=. --public. Specify a remote with the --remote flag. To push your commits, pass the --push flag. For more information about possible arguments, see the GitHub CLI manual.
🌐
Reddit
reddit.com › r/git › pushing to github via the terminal
r/git on Reddit: Pushing to GitHub via the terminal
March 31, 2023 -

Hey, I’m a new software developer looking to build a portfolio. When pushing to GitHub via the terminal, am I supposed to push the root folder of my project onto the repository? Or is it specific files or sub folders? Are there any best practices for having a presentable repository? And lastly, what do I add in the ReadMe file?

Find elsewhere
🌐
Vercel
vercel.com › vercel documentation › build & deploy › git integrations › github
Deploying GitHub Projects with Vercel
March 17, 2026 - The Deploying a Git repository guide outlines how to create a new Vercel Project from a GitHub repository, and enable automatic deployments on every branch push.
🌐
Scribd
scribd.com › document › 945345340 › Push-Code-to-GitHub
How to Push Code to GitHub Repo | PDF
JavaScript is disabled in your browser · Please enable JavaScript to proceed · A required part of this site couldn’t load. This may be due to a browser extension, network issues, or browser settings. Please check your connection, disable any ad blockers, or try using a different browser
🌐
Hashnode
nomadev.hashnode.dev › push-code-to-github-without-using-the-command-line-in-just-2-steps
How to Push code to GitHub without using the command line
November 19, 2021 - After you have published your repository to GitHub, you can do whatever you can do with the CLI (i.e commit, push, pull, etc) from the Source Control Section (Ctrl + Shift + G), by just having single clicks instead of writing long commands.
🌐
GitHub
gist.github.com › xirixiz › b6b0c6f4917ce17a90e00f9b60566278
Set up GitHub push with SSH keys · GitHub
So following those steps will work?Sent ... GitHub push with SSH ***@***.*** commented on this gist.I cant get past the test part. I type this in : ssh -T ***@***.*** it said Permission Denied (Public Key)Don't forget to ensure the SSH agent is already running and that your GitHub private SSH key has been added to the agent. There is relevant GitHub documentation that provides more details on how to start the ...
Top answer
1 of 1
5

I have my code in a local repo ...

You also have an existing GitHub repository with some set of commits in it? (This is a question, but I'm going to assume that the answer is "yes" based on the rest of the text above this point.)

Because a Git repository is a collection of commits, and your goal is going to be to add commits (not make independent ones), you will need to start by cloning the existing repository, not making a new empty one. So this part is wrong:

git init

as that's used to make a new empty repository. (It's not completely wrong, nor irrecoverably wrong: git clone starts by running git init! We'll have a look in a bit at what you can to do recover.)

Meanwhile, I'll answer this question first:

What have I done wrong?

git add .
git commit -s -m <my-commit-message>

This part is OK, except for one problem: if you created a new, empty repository with the git init step, this made a single initial commit in this new, previously-empty repository. That's not what you wanted. You wanted to add a new commit to an existing repository.

git remote add origin <repo-URL>

(This is also something git clone would have done for you. You do need this step, but using git clone would make it easier.)

git branch -M main

This is only necessary if you made a new, empty repository earlier and when you made your first (and now only) commit, Git chose to create the branch name master, but you want to use the name main. You probably don't want any of this, really; when you use git clone, you'll get one branch of your own, and it will be based on the branch names in the repository you're cloning, which will take care of all of this.

git pull // not sure if this is necessary

What git pull does is two-fold:

  1. It runs git fetch. That, too, is something git clone would have done for you. This is appropriate to do.

  2. It runs a second Git command to merge or rebase your work (your new commits) with / onto some existing work ("their" commits as obtained by the git fetch step). The default action here is git merge and that's not what you wanted, but in any case, pull requires an upstream setting and you won't have one, so this second command just fails entirely. That means it causes no harm either.

git checkout -b feat/dev // the remote branch name

The git checkout -b option creates a new branch name without using any remote-tracking names. This, too, is not what you want. This created a new branch name for your lone root commit: you now have main and feat/dev.

git push -uf origin:feat/dev main

This won't work at all: the syntax for git push is:

git push <remote> <refspec>

and in your case, the remote part is just plain origin. That's the name you gave to your git remote add above.

The somewhat scary term refspec is Git jargon, and for most users of git push, is overkill: what goes here is simply a branch name.

When that didn't work ...

git push -uf origin main

Be careful with -f! This will overwrite any existing branch on the other (GitHub) end, if they accept the force option. If this is your own fork of some GitHub repository, though, you won't actually damage anything other than your own fork. (But you might damage your fork: if there was a main there before, now it's gone, replaced with your lone commit.)

Code gets pushed to a new branch named main and I cannot open a pull request for the 2 branches have entirely different commit histories.

Right: this sent, to your GitHub repository, your lone commit on main.

Fixing the mess

The exact fix will depend on several things:

  • Do you have your own fork?

    • If not, was there a main in the GitHub repository before?
    • If so, did you wreck it?

    If there was a main and you wrecked it, and it was your own fork, you can definitely recover.

    If there was a main and you wrecked it and this was a shared GitHub repository, someone else may need to fix it. We'll leave the "shared repository, someone else needs to fix it" out entirely.

In any case, let's start by making a new clone. This isn't really required—we could do all the fixing-up in your existing clone—but it's probably the best way to go. To make a new clone of your GitHub fork, change to a new empty directory / folder on your laptop (or whatever computer you're using) and run:

git clone <github-url>

This does the git init / git remote add / git fetch stuff for you, creating a new Git repository by taking the URL and stripping off any final .git and leading slash stuff. By doing the clone into an empty directory, when the clone finishes, you'll have just one sub-directory here with one name, and that will be the new clone, so it will be really easy to find. (You can move it afterward.)

Your new clone has copied all the commits from the GitHub repository, which I'm assuming here is your own fork. Now, if you wrecked your fork's main, the next step is to fix that, and to do that we need to use git remote add to add the shared repository as a second remote. This part gets a little complicated and is only going to be used here for the fixing purpose:

git remote add r2 <github-url>    # using the URL for the *shared* repo
git fetch r2                      # get all of THEIR commits
git push -f origin r2/main:main   # replace your fork's main with r2's main
git remote rm r2                  # clean up to remove complications

(If you don't need to fix anything up, you can skip this set of commands entirely. The git remote rm r2 step above tore down all the extra stuff and your fork should be back to the way it was before you wrecked it. If you wrecked a shared GitHub repo, get someone else to fix it as there's no guarantee that any of your own repositories have the right stuff in them any more, but someone else is almost certain to have what's needed.)

Now that you have a good clone, you can get to work

What you need to know:

  • A Git repository is basically a big collection of commits.
  • Each commit stores a full snapshot of every file (frozen for all time so that one can get them all back, provided you have the right hash ID for the commit), plus some metadata, or information about that commit itself, such as who made it and when.
  • A Git repository also contains a collection of names: branch names, tag names, and something Git should call remote-tracking names. (Git actually calls them remote-tracking branch names, but the word branch here is, I think, just a bad idea.)

Git actually finds commits by their big, ugly, random-looking hash IDs. These are impossible for humans to work with, so we just don't. We use things like branch names. What the branch names do—what Git gives us when we use them—is that the name turns into the right hash ID. Git needs the hash ID. We give it a name (main or feat/dev) and Git looks up the hash ID from that.

Now, the tricky part with git clone is this:

  • git clone creates a new, empty repository (git init) that has no commits and no branch names;
  • then git clone adds a remote, named origin, and does a git fetch, which gets all of some other repository's commits; but
  • git clone does not create the same branch names in the new repository!

Instead, Git takes each of their (the remote's) branch names, like main and develop and feat/ure, and renames them. Your own Git repository, on your laptop, winds up with names like origin/main and origin/develop and origin/feat/ure. Each of these is a remote-tracking name and it's just a renamed copy of the other guy's branch names.

When you run git fetch, your Git (your Git software running with your repository) reaches out to their Git (their software on their repository, whoever "they" are over at origin) and finds any new commits they have that you don't. Your Git brings those into your repository. Every commit gets a unique hash ID, so these have new unique IDs, never used before; they will never be used for any future commit either, so that the IDs Git sees here now are the right IDs forever. Your Git stuffs the new commits into your repository and updates your remote-tracking names.

This way, your origin/main holds a memory of where their main was, the last time your Git reached out to their Git. Your remote-tracking names remember their branch names: both the name (with origin/ stuffed in front) and the hash ID. But these are not your branch names. That means you can create a branch name and not worry that it will get overwritten if someone else creates the same name in their repository.1 Their branch hello becomes origin/hello, which can't possibly collide with your branch hello. Just don't name any of your branches origin/whatever.2

Having run the initial git init, git remote add, and git fetch, your git clone now does one last thing: it creates one branch name for you. The branch name that git clone creates, in your new clone, is the one you specify with your git clone command's -b option, when you run:

git clone -b xyz <url>

But, wait a minute, we didn't run with a -b option. Now what?

Well, absent the -b option, your Git software asks their (origin's) software which branch name their repository suggests. That's almost always main these days (and used to be master). So that's what comes out as the recommendation, and that's the one branch name your Git creates, in your repository.

A branch name must select some particular commit. So the commit your Git chooses is the one named by your remote-tracking name that corresponds to the branch name here. If they recommended main, you have an origin/main remote-tracking name, remembering their main. That name specifies one particular commit. That's the one particular commit your Git uses to make your name main.

The end result is that you get one branch in your new clone, matching one branch in the repository you cloned. It's all a very roundabout and complicated way of doing something ridiculously simple—which is a good description of a whole lot of things that Git does.


1When using GitHub forks, this is not a real problem. But if you use Git the way it was before GitHub came along, it is a real problem: you might pick a branch name like fix-xyz-command, and Bob might pick the same name for a different fix for that command. So in real world situations, this was a problem, and remote-tracking names solved it. Within the GitHub world, it almost never comes up, though there are more complicated situations where it still can.

2This is technically not a problem either, because your local branch names are in a separate name space. But while Git won't get confused if you name a local branch origin/main, you probably will, so just don't do it.


Grabbing the code you worked on earlier

You're probably ready now to create your feature branch in your new clone:

git switch -c feat/dev

or:

git checkout -b feat/dev

These both do exactly the same thing: they create a new name, feat/dev, that selects the same commit you have out right now (the one from main, probably). Then, having created this new name, they switch to that branch.

Now you need to get the files from the other repository you made. There are several ways to do that, but one somewhat magical one is this:

git --work-tree=/path/to/wrong/repo add .

A more direct way is to just copy the files from the "wrong" repository's working tree into the new one's working tree, and then run git add . as you did earlier.

I always like to run:

git status

and then:

git diff --staged

at this point, to check that I have the right files "staged for commit", and see what I have changed. Only then, having reviewed my work and checked it over for any obvious errors, do I run:

git commit

(or git commit -s3) and write up a commit message. This updates the current branch (now feat/dev) with the new commit, and now I can run:

git push -u origin feat/ure

to create branch feat/ure in my GitHub fork and make a pull request on GitHub (using gh, the GitHub CLI, or using the web site).


3The -s option makes a signed commit. With any Git commit you make, or that anyone makes, anyone can claim to be anyone they want. I can make commits that claim to be authored by Barack Obama or Volodomyr Zelenskyy. Some people like to add digital signatures, using PGP or GPG or ssh digital signature techniques. These can (in the absence of quantum computers at least) serve to verify that these are in fact by you and not by some imposter. However, I find signed commits to be serious overkill and a pain; I prefer the Git project's method of simply signing official release tags.

🌐
Zapier
zapier.com › productivity › app tutorials
How to push to GitHub | Zapier
April 24, 2025 - If you haven't already done so, GitHub will prompt you to authenticate your identity. Enter your GitHub username and password. Once the authentication is done, the upload process will start. After it's complete, you'll see specific details about where the data was uploaded. That's it! You've successfully pushed your local files and folders to your GitHub repository.
🌐
X
x.com › vikhyatk › status › 2048290071498109055
i was not impacted by the github issue, because i don't ...
This Reddit post screenshot has been edited to promote @vikhyatk's AI company. The original post links to the project "Sherlock". reddit.com/r/github/comme…
🌐
Git Tower
git-tower.com › learn › git faq › how to push to github
How to Push to GitHub | Learn Version Control with Git
2 weeks ago - In case you're using a desktop ... your local repository · from now on, you can simply use the "Push" button in the toolbar to upload new changes to the remote on GitHub...
🌐
Barbagroup
barbagroup.github.io › essential_skills_RRC › git › github1
push and pull - Essential skills for reproducible research computing
Branch master set up to track remote branch master from origin. To github.com:gforsyth/wordcount.git * [new branch] master -> maste · Again, that's a bunch of information. What did we do? We told git to push from master on the local machine, to a branch called master on the remote (which is called origin).
🌐
Everhour
everhour.com › github › how to push to github: complete step-by-step guide
How to Push to GitHub from VS Code or Terminal
December 22, 2025 - Use git add, git commit, then git push to update the repo. Initialize Git in the folder and connect it to your GitHub repo.
🌐
Continuously Merging
articles.mergify.com › how-to-push-to-github-a-short-guide
How to Push to GitHub: A Short Guide
August 16, 2023 - To learn how to push files to GitHub, you’ll first need to get Git set up and running on your machine. Once that has been done, you can instruct Git to recognize your remote repository (referred to as a “remote” in Git) and tell it to update it as needed.
🌐
GitHub
gist.github.com › mindplace › b4b094157d7a3be6afd2c96370d39fad
Pushing your first project to github · GitHub
Copy the link in the input right beneath the title, it should look something like this: https://github.com/yourname/yourproject.git This is the web address that your local folder will use to push its contents to the remote folder on Github.
🌐
opencode
opencode.ai › docs › github
GitHub | OpenCode
3 days ago - This will walk you through installing the GitHub app, creating the workflow, and setting up secrets. Or you can set it up manually. ... Head over to github.com/apps/opencode-agent.