To push your local codebase to GitHub, follow these steps with the complete set of Git commands:
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:
Initialize the Git Repository (if not already done):
git initAdd 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>Stage All Changes:
git add .Commit the Changes:
Replace"Your commit message"with a meaningful commit message.git commit -m "Your commit message"Push Code to GitHub:
Use the-uflag to set the upstream branch for easier future pushes.git push -u origin main
Switching to an Existing Branch:
List all branches:
git branchThis will show all local branches. The current branch will have an asterisk (
*) next to it.Switch to the desired branch:
git switch branch-nameOr, if you're using an older version of Git:
git checkout branch-name
Creating and Switching to a New Branch
Create and switch to a new branch in one step:
git switch -c new-branch-nameOr, with older Git versions:
git checkout -b new-branch-namePush the new branch to the remote repository:
git push -u origin new-branch-name
Switching to a Remote Branch
Fetch all branches from the remote:
git fetchCheck out the remote branch:
git checkout branch-nameIf you’re using Git 2.23 or newer, you can also use:
git switch branch-name
Notes:
Replace
mainwithmasteror 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).