Push to GitHub Using Git Bash



Git is a fast, scalable, distributed revision control system with an unusually rich command set that provides both high-level operations and full access to internals. Here is a step-by-step guide with examples that explains how you can push your code to GitHub using Git Bash, with creating repository to updating the code and pushing the changes back to GitHub using Git Bash.


1. Install Git Bash

  • Download Git Bash from the official Git website.
  • Run the installer and follow the setup instructions.
  • Once installed, open Git Bash by searching for it in the start menu or by right-clicking on the desktop and selecting Git Bash Here.

If this is your first time using Git, configure your username and email. Git uses this information to track changes made by you.

git config --global user.name "your name"
git config --global user.email "[email protected]"


3. Create a GitHub Repository

Go to GitHub.com and sign in.


Click on the New button in the top right corner of repositories tab.



Enter the name of your repository and choose visibility (public or private).




Click on Create Repository.



4. Set up Repository locally

Once repository is created to create local copy of that you need to clone the repository. For that click on the repository created and copy the HTTPS url



Open git bash in your local system and run the following command in the path you want the repository to be cloned

git clone <Https url copied>



 Now local copy is created and you can add anything to the repository. Then to update the changes back to GitHub follow the steps below.


 Initialize a Local Repository

 Navigate to your project folder using Git Bash, if your are in different path.

cd /path/folder-name


5. Add Files to the Staging Area


You can check the status of the repository after adding new changes using following command.

git status



It will show the files which are not committed. To add all the files in directory to the staging area, run following command

git add .


Alternatively, to add specific file run

git add filename.txt
The staging area is a place where you prepare files before committing.


6. Commit Your Changes

Now that your files are in the staging area, commit them with a message describing the changes.

git commit -m "Initial commit"


Committing saves a snapshot of the project at this stage, so you can refer to it later. Here -m indicates commit message.

7. Push to GitHub

Now, push your changes to GitHub. Use the following command to push to the main branch

git push -u origin main


If your branch is named something other than main, replace main with your branch name.

8. Verify on GitHub

Go to your GitHub repository page, and you should see all your files uploaded.



Additional Git Commands

Creating a New Branch:  If you want to create a branch and push it to GitHub:

git checkout -b <branchname>
git push -u origin <branchname>
Viewing Commit History: To check the commit history run the command
git log
Pulling Changes: If you want to get the latest code changes from remote repository to local run the following command
git pull origin main

Conclusion

By following the steps given in this tutorial, you can easily create a repository and update code in local and push changes to remote repository in GitHub using GitBash.

Geethanjali Gandu
Geethanjali Gandu

Tech Enthusiast

Updated on: 2024-11-12T11:43:27+05:30

162 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements