Skip to main content

Command Palette

Search for a command to run...

To push your local codebase to GitHub, follow these steps with the complete set of Git commands:

Published
•2 min read•View as Markdown
D

Hey! I'm Deepak, MCA student of Chandigarh University. I'm more interested in Cloud Computing and Devops automations tools like Docker, Kubernetes, CICD pipelines etc.. I'm also doing my project and looking for open-source contribution. Good hands-on knowledge of Source Code Management (Version Control System) tools like Git.

Full Command Sequence:

  1. Initialize the Git Repository (if not already done):

     git init
    
  2. Add Remote Repository URL:
    Replace <repository-url> with your GitHub repository link (e.g., https://github.com/username/repository.git).

     git remote add origin <repository-url>
    
  3. Stage All Changes:

     git add .
    
  4. Commit the Changes:
    Replace "Your commit message" with a meaningful commit message.

     git commit -m "Your commit message"
    
  5. Push Code to GitHub:
    Use the -u flag to set the upstream branch for easier future pushes.

     git push -u origin main
    

Switching to an Existing Branch:

  1. List all branches:

     git branch
    

    This will show all local branches. The current branch will have an asterisk (*) next to it.

  2. Switch to the desired branch:

     git switch branch-name
    

    Or, if you're using an older version of Git:

     git checkout branch-name
    

Creating and Switching to a New Branch

  1. Create and switch to a new branch in one step:

     git switch -c new-branch-name
    

    Or, with older Git versions:

     git checkout -b new-branch-name
    
  2. Push the new branch to the remote repository:

     git push -u origin new-branch-name
    

Switching to a Remote Branch

  1. Fetch all branches from the remote:

     git fetch
    
  2. Check out the remote branch:

     git checkout branch-name
    

    If you’re using Git 2.23 or newer, you can also use:

     git switch branch-name
    

Notes:

  • Replace main with master or the branch name you're using in your GitHub repository.

  • If your repository is private or requires authentication, GitHub will prompt for credentials (username and personal access token or SSH key).