Push a Local Git Branch to a Remote GitHub repo |Video upload date:  · Duration: PT4M51S  · Language: EN

Quick guide to push a local Git branch to a remote GitHub repo with commands for adding remote and setting upstream.

If you have local work sitting in a branch and you want to send it to GitHub without causing a small office incident follow along. This short command line tutorial covers creating or switching to a git branch staging and committing your changes adding a remote if needed and pushing the branch so other humans can review your glorious code.

Create or switch to a branch

Make a branch name that actually explains what you did unless you enjoy future archeological digs. To create and switch in one step run

git checkout -b my-branch

To switch to an existing branch do

git checkout my-branch

Use readable names that help your team and your future self. This is version control not treasure hunting.

Stage and commit changes

Stage everything or pick files like a responsible developer. On the command line use:

  • git add . to stage all changes
  • git add path/to/file to stage specific files

Then commit with a clear message that explains what changed. For example

git commit -m "Describe the change briefly"

Good commit messages save hours of confusion later. Bad commit messages are a public service announcement for chaos.

Check or add a remote

If your local repository has already been linked to a remote you will see it with

git remote -v

If nothing shows up then add your GitHub repo as a remote. Replace the URL placeholder with your repo URL

git remote add origin <your-repo-url>

That configures the git-remote named origin which is the usual shorthand for the remote repository on GitHub.

Push the branch and set upstream

To publish the branch and set upstream tracking so future pushes and pulls are simpler run

git push -u origin my-branch

The -u option tells Git to remember that origin/my-branch is the upstream for your local branch. After that you can just run git push or git pull without repeating names.

Authentication and permissions

If GitHub refuses your push with a permission error authenticate using an SSH key or a personal access token depending on your setup. Both methods work. If you get stuck the GitHub docs have step by step guides and your keyboard can be used for pleading.

Quick safety tips for the command line

  • Run git status before pushing to confirm you are on the right branch and have staged the intended files
  • If you are tempted to use force push think twice and consider rebasing locally then pushing a fresh branch
  • Use descriptive branch names and clear commit messages to keep the repository history useful

When collaborators appear

Once the branch is on GitHub you can open a pull request or ask a reviewer to look. Pushing a branch is the minimum viable collaboration step in modern version control. Congratulations you have shared code with the world and possibly started a lively code review conversation.

That is all you need to push a local git-branch to a remote repository on GitHub using the command-line. Follow these steps and you will avoid the most common mistakes while keeping your workflow sane and slightly less dramatic.

I know how you can get Azure Certified, Google Cloud Certified and AWS Certified. It's a cool certification exam simulator site called certificationexams.pro. Check it out, and tell them Cameron sent ya!

This is a dedicated watch page for a single video.