Command guide

How to Push Code to GitHub With Git Commands (Step by Step)

By Admin · Updated · 10 min read · Checked against the official Git docs

To push code to GitHub, run these Git commands from your project folder: git init, git add ., git commit -m "First commit", git remote add origin <url>, then git push -u origin main. After that first push, sending new work is just git add, git commit and git push.

Quick answer

New project: git init → git add . → git commit -m "First commit" → git branch -M main → git remote add origin https://github.com/you/repo.git → git push -u origin main. Project already on GitHub: git add . → git commit -m "msg" → git push.

Git commands to push code to GitHub: git add, git commit and git push -u origin main

How we tested: every command on this page was run in a real Git repository (Git 2.43) on 19 November 2026, pushing to a bare repository that stands in for GitHub. The output is copied from the terminal. A push to GitHub prints the same lines plus a few progress lines (“Enumerating objects”, “Writing objects”), and your commit hashes will differ.

The first time you push code to GitHub, it trips up almost everyone. Not because the Git commands are hard, but because there are three separate jobs hidden inside “push my code”: saving your work as a commit, telling Git where GitHub is, and proving to GitHub that you’re allowed in. This guide walks through each one in order, shows you what the terminal should print at every step, and fixes the errors you’re most likely to hit.

Before you start

You need three things in place before the first push:

  • Git installed. Run git --version. If you see a version number, you’re set. If not, our beginner’s guide to Git covers installing it on Windows, macOS and Linux.
  • Your name and email set. Every commit is stamped with them. Run git config --global user.name "Your Name" and git config --global user.email "you@example.com" once per computer. Use the email on your GitHub account so commits link to your profile.
  • An empty repository on GitHub. On github.com, click New repository, give it a name, and leave “Add a README”, “.gitignore” and “license” unticked. An empty repository makes the first push clean. If you do add a README there, see the rejected push fix below.

How to push code to GitHub for the first time, step by step

Open a terminal in your project folder (Git Bash on Windows, Terminal on macOS, or the terminal inside VS Code). Then run these Git commands in order.

1. Turn the folder into a Git repository

$ git init
Initialized empty Git repository in /projects/my-app/.git/

This creates a hidden .git folder where Git keeps your history. Nothing is tracked yet. Skip this step if the folder is already a repository (if git status works, it is).

2. Check what Git sees

$ git status
On branch main

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)
	README.md
	app.js

nothing added to commit but untracked files present (use "git add" to track)

Read this list before you go further. If you see files that shouldn’t be public, such as .env, passwords, API keys or a node_modules folder, add them to a .gitignore file now. Once a secret is pushed to GitHub, assume someone has seen it.

3. Stage your files

$ git add .

The dot means “everything in this folder”. Staging is like putting items in a box before you seal it. You can stage single files with git add app.js if you only want some of them in this commit.

4. Commit

$ git commit -m "First commit"
[main (root-commit) 437b27a] First commit
 2 files changed, 2 insertions(+)
 create mode 100644 README.md
 create mode 100644 app.js

A commit is a saved snapshot of everything you staged. Git can only push commits, so if you skip this step the push has nothing to send.

5. Make sure your branch is called main

$ git branch -M main

GitHub names the default branch main. Older Git installs may call yours master. This command renames the current branch to main so the two match. It prints nothing when it works. To make main the default for every new repository, run git config --global init.defaultBranch main.

6. Connect your project to GitHub

$ git remote add origin https://github.com/you/my-app.git
$ git remote -v
origin	https://github.com/you/my-app.git (fetch)
origin	https://github.com/you/my-app.git (push)

Copy the URL from the green Code button on your GitHub repository page. origin is just a nickname for that URL, so you don’t have to type it every time. git remote -v confirms it was saved.

7. Push

$ git push -u origin main
To https://github.com/you/my-app.git
 * [new branch]      main -> main
branch 'main' set up to track 'origin/main'.

* [new branch] means the branch now exists on GitHub. Refresh the repository page and your files will be there. The -u flag links your local main to origin/main, so from now on a plain git push or git pull knows where to go.

The everyday loop: push code to GitHub after every change

Once the first push is done, you’ll repeat the same three Git commands for every change:

$ git add .
$ git commit -m "Fix greeting"
[main fd029ff] Fix greeting
 1 file changed, 1 insertion(+)
$ git push
To https://github.com/you/my-app.git
   437b27a..fd029ff  main -> main

The line 437b27a..fd029ff shows GitHub moving from your old latest commit to the new one. Afterwards, git status should say:

$ git status
On branch main
Your branch is up to date with 'origin/main'.

nothing to commit, working tree clean

“Up to date with origin/main” plus “working tree clean” means everything on your machine is safely on GitHub. If you only changed files Git already tracks, git commit -am "message" stages and commits in one step, but it skips brand-new files, so git add . is the safer habit.

Git commands to push code: summary table

Situation Git command
Start tracking a folder git init
Stage every change git add .
Save a snapshot git commit -m "message"
Rename branch to main git branch -M main
Connect to GitHub git remote add origin <url>
Fix a wrong remote URL git remote set-url origin <url>
First push of a branch git push -u origin <branch>
Every push after that git push
Get teammates’ work first git pull --rebase

For the rest, from undoing a commit to tagging a release, see the full list of Git commands on our homepage.

Logging in: GitHub doesn’t accept your password

The first time you push, Git asks for a username and password. Here’s the catch: since August 2021, GitHub rejects your account password for Git over HTTPS. You have three options:

  • Use a credential manager (easiest). Git for Windows and GitHub Desktop include Git Credential Manager, which opens a browser window so you can sign in once. If you have the GitHub CLI, gh auth login does the same job.
  • Use a personal access token. Create one in GitHub under Settings → Developer settings → Personal access tokens, give it access to your repository, and paste it when Git asks for a password.
  • Use SSH. Generate a key with ssh-keygen -t ed25519 -C "you@example.com", add the public key in GitHub under Settings → SSH and GPG keys, and use the SSH URL: git remote set-url origin git@github.com:you/my-app.git. No more prompts after that.

GitHub’s own guide to remote URLs explains the trade-offs between HTTPS and SSH in more depth.

Common errors when you push code to GitHub (and fixes)

“Updates were rejected because the remote contains work that you do not have locally”

$ git push
To https://github.com/you/my-app.git
 ! [rejected]        main -> main (fetch first)
error: failed to push some refs to 'https://github.com/you/my-app.git'
hint: Updates were rejected because the remote contains work that you do not
hint: have locally. This is usually caused by another repository pushing to
hint: the same ref. If you want to integrate the remote changes, use
hint: 'git pull' before pushing again.

GitHub has commits you don’t: a teammate pushed, you edited a file on github.com, or you created the repository with a README. Git refuses to overwrite them, which is a good thing. Bring their work in first, then push:

$ git pull --rebase
Successfully rebased and updated refs/heads/main.
$ git push
To https://github.com/you/my-app.git
   66c2180..aba805a  main -> main

--rebase puts your commits on top of theirs, so history stays a straight line. If both of you changed the same lines, Git stops and asks you to choose; our guide to resolving merge conflicts shows how. The difference between pulling and fetching is covered in git fetch vs git pull.

Whatever you do, don’t reach for git push --force to make this error go away. It replaces what’s on GitHub with your copy and can wipe out a teammate’s work.

“The current branch has no upstream branch”

$ git push
fatal: The current branch feature/login has no upstream branch.
To push the current branch and set the remote as upstream, use

    git push --set-upstream origin feature/login

You’re on a branch that doesn’t exist on GitHub yet. Run git push -u origin feature/login (-u is short for --set-upstream). To stop seeing this for every new branch, run git config --global push.autoSetupRemote true once. Our guide on creating a new branch in Git covers the full branch workflow.

“error: src refspec main does not match any”

Git can’t find a branch called main to push. Either you haven’t made a commit yet (run git add . and git commit first), or your branch is called master. Check with git branch, then rename with git branch -M main or push the real name with git push -u origin master.

“remote origin already exists”

The folder is already connected to a remote, perhaps one you cloned from or typed wrong. Point it at the right repository instead of adding a new one: git remote set-url origin https://github.com/you/my-app.git.

“Authentication failed” or “Permission denied (publickey)”

The first means GitHub refused your password; use a token or credential manager as described above. The second means your SSH key isn’t added to GitHub, or the repository belongs to an account your key isn’t linked to. Run ssh -T git@github.com to check which account GitHub recognises.

“File is larger than 100 MB”

GitHub blocks single files over 100 MB. Remove the file from your last commit (our guide to removing a file from a commit walks through it), add it to .gitignore, and use Git LFS if you really need large files in the repository.

Pushing to a repository you cloned

If you started with git clone https://github.com/you/my-app.git, Git already set up origin and tracking for you. Skip git init, git remote add and -u completely. To push code to GitHub from a clone, edit your files, then run git add ., git commit -m "message" and git push. That’s the whole loop.

If the clone belongs to someone else and you don’t have write access, the push will fail. Fork the repository on GitHub, clone your fork, push there, and open a pull request.

Three habits that save you later

Run git status before every commit. It takes a second and catches the stray file you didn’t mean to share.

Write commit messages a stranger could understand. “Fix login redirect after password reset” beats “update”. If you get one wrong, you can change a commit message, even after pushing.

Pull before you start work. A quick git pull each morning means fewer rejected pushes and smaller conflicts. And if you commit something by mistake before pushing, undoing the last commit takes one command.

FAQ: pushing code to GitHub

What are the Git commands to push code to GitHub?

For a new project: git init, git add ., git commit -m "First commit", git branch -M main, git remote add origin <url> and git push -u origin main. For later changes: git add ., git commit -m "message" and git push.

What does git push -u origin main mean?

It pushes your local main branch to the remote called origin (your GitHub repository) and, because of -u, remembers that link. After that, git push and git pull work without extra arguments.

Do I need to commit before I push?

Yes. git push only sends commits. Changes that are edited but not committed, or staged but not committed, stay on your computer.

Why does GitHub reject my password?

GitHub stopped accepting account passwords for Git over HTTPS in August 2021. Sign in through Git Credential Manager or gh auth login, paste a personal access token instead of your password, or switch to SSH.

Can I push code to GitHub from VS Code?

Open the built-in terminal (View → Terminal) and run the same Git commands shown above. VS Code’s Source Control panel runs these commands for you behind the buttons, so the result is identical.

Is it safe to use git push –force?

Only on a branch nobody else uses. It overwrites the GitHub copy with yours and can delete other people’s commits. If you must, use git push --force-with-lease, which refuses when someone else has pushed in the meantime.

Written by Admin

Writes practical Git guides for Git Commands. Every command is checked against the official Git documentation.

About Git Commands