Basic Git Series: Pushing Your Code to GitHub

Adam Sturge | Aug 15, 2023 min read

Overview

After making changes locally, you need to push them to a remote repository to share your work or back it up.

Steps

  1. Stage your changes for commit:
git add .           # Adds all modified files
# or
git add filename    # Adds specific file
  1. Commit your changes with a descriptive message:
git commit -m "Brief description of your changes"
  1. Push your commits to the remote repository:
git push origin branch-name

Common Issues

  • Rejected push: The remote branch has changes you don’t have locally. Perform a git pull first
  • Authentication failed: Ensure your credentials are correct; consider using SSH or a personal access token
  • New branch not appearing on GitHub: For a new branch, you may need to set upstream tracking:
    git push -u origin branch-name
    

Additional Notes

  • Always pull before pushing to avoid conflicts
  • Use meaningful commit messages that explain why the change was made
  • If you’ve created a branch locally, you need to push it to make it visible remotely

Making Changes Directly on GitHub

Instead of pushing local changes, you can make simple edits directly on GitHub:

  1. Navigate to the repository and file you want to edit
  2. Click the pencil icon (Edit this file)
  3. Make your changes in the editor
  4. Scroll down to the “Commit changes” section
  5. Add a commit message describing your changes
  6. Choose whether to commit directly to the current branch or create a new branch and pull request
  7. Click “Commit changes”

This is useful for small changes but lacks the power of a local development environment.