DEV Community

Cover image for GitHub 101: Cloning, Committing, and Creating New Branches
Shubhankar Valimbe
Shubhankar Valimbe

Posted on

GitHub 101: Cloning, Committing, and Creating New Branches

Hello beautiful people!

Today I'm going to act as a chaperone on your journey to become a GitHub pro. If you're an experienced dev, you already know what it is. For the ones that don't, GitHub is a widely used platform for collaborating on code. This guide will walk you through the process of cloning a GitHub repository, making changes, and pushing those changes to both the main branch and a new branch.

Before starting, please create an account on GitHub if you don't already have one and install Git.


Cloning a GitHub Repository

To get started, you'll need to clone an existing GitHub repository to your local machine. Follow these steps:

Navigate to directory

Open your terminal or command prompt.

Navigate to the directory where you want to store the cloned repository:

cd /path/to/your/desired/directory
Enter fullscreen mode Exit fullscreen mode

Copy Repo URL

Click on the Code button in the upper right corner of repo files and copy the HTTPS URL.

Code Button

Clone URL

Clone the repo

Clone the repository using its HTTPS URL. Replace username and repository-name with the actual URL of the GitHub repository:

git clone https://github.com/username/repository-name.git
Enter fullscreen mode Exit fullscreen mode

Authenticate GitHub credentials

Git will prompt you for your GitHub username and password (or personal access token, if two-factor authentication is enabled) to authenticate and clone the repository.

Once the clone is complete, you'll have a local copy of the Git repository in the directory you specified.


Making Changes

With the repository cloned locally, you can now make changes to the code or files. You can edit, add, or delete files as needed.


Pushing Changes to the Main Branch

To commit and push your changes to the main branch, follow these steps:

Stage the changes

Stage the changes you want to commit. To stage all changes, use:

git add .
Enter fullscreen mode Exit fullscreen mode

Commit Changes

Commit the changes with a descriptive message:

git commit -m "Your commit message here"
Enter fullscreen mode Exit fullscreen mode

Push Changes

Push the changes to the main branch on GitHub:

git push origin main
Enter fullscreen mode Exit fullscreen mode

Creating and Pushing to a New Branch

To create and push your changes to a new branch, do the following:

Create a branch

Create a new branch locally and switch to it:

git checkout -b new-branch-name
Enter fullscreen mode Exit fullscreen mode

Make and commit your changes in this new branch as shown above.

Push to new branch

Push the new branch to GitHub and set up tracking:

git push -u origin new-branch-name
Enter fullscreen mode Exit fullscreen mode

With these steps, you've successfully cloned a GitHub repository, made changes, and pushed those changes both to the main branch and a new branch. This workflow allows you to collaborate effectively on GitHub projects.

Let me know your thoughts or questions in the comments!

Top comments (0)