simple Git and GitHub ๐Ÿ’–

simple Git and GitHub ๐Ÿ’–

ยท

7 min read

I consider you a beginner according to that we will explore this in detail. Let's say you're working on a website and you want to add a new feature to it. Before making any changes to the website's code, you create a new branch in Git. This branch is a copy of the website's code, so you can experiment with different changes without affecting the main code. Let's call this branch "new-feature". You then make changes to the "new-feature" branch, adding the new feature to the website. You test the feature and make any necessary adjustments until you're happy with the results.

Once you're satisfied with the new feature, you merge the "new-feature" branch back into the main code using Git. This ensures that the changes you made are incorporated into the website's code.

But what if you want to share your new feature with others or collaborate with other developers on the website? This is where GitHub comes in. You can create a repository on GitHub and upload your code to it. Other developers can then clone your repository (make a copy of it) and make their own changes to the code.

If someone makes changes to your code that you like, you can merge their changes back into your code using Git. If you don't like their changes, you can revert back to an earlier version of the code using Git.

Git and GitHub allow you to keep track of changes to your code, experiment with new features, collaborate with other developers, and ensure that your code is up-to-date and bug-free.

it is a distributed version control system that allows multiple people to work on the same codebase at the same time without stepping on each other's toes. GitHub is a web-based hosting service for Git repositories that makes it easier for teams to collaborate on software projects. In this blog post, we'll go over the basic concepts of Git and GitHub and provide instructions on how to use Git and GitHub from the command line on Windows, macOS, and Linux.

Git basics

Before we dive into using Git and GitHub, let's go over some basic concepts.

Repository

A repository, or repo for short, is a collection of files that you want to keep track of using Git. A repo can contain code, images, documents, and any other type of file. When you initialize a repo, Git creates a .git directory in the root directory of your repo that contains all of the necessary files and metadata to track changes to your files over time.

Commit

A commit is a snapshot of the changes that you've made to your files since the last commit. Each commit has a unique identifier, called a hash, that you can use to reference it later. When you commit changes to your repo, Git stores the changes in the .git directory and updates the pointer to the latest commit.

Branch

A branch is a parallel version of your repo that allows you to work on new features or bug fixes without affecting the main branch. When you create a branch, Git creates a copy of the main branch and allows you to make changes to it independently. Once you've completed your work on the branch, you can merge it back into the main branch to incorporate your changes.

Merge

Merging is the process of combining two branches into one. When you merge a branch into the main branch, Git combines the changes that you've made on the branch with the changes that have been made on the main branch since you created the branch.

Git command-line interface

Git can be used from the command line on Windows, macOS, and Linux. In this section, we'll provide instructions on how to use Git from the command line on each operating system.

Configure tooling

Configure user information for all local repositories

$ git config --global user.name "[name]"

Sets the name you want to be attached to your commit transactions

$ git config --global user.email "[email address]"

Sets the email you want to be attached to your commit transactions

$ git config --global color.ui auto

Enables helpful colorization of the command line output

Windows

To use Git from the command line on Windows, you'll need to install Git for Windows, which includes a Git command-line interface.

  1. Download Git for Windows from the official Git website: https://git-scm.com/

  2. Run the installer and follow the instructions.

  3. Open a command prompt or Git Bash terminal.

  4. Navigate to the directory where you want to initialize your repo.

  5. Run the following command to initialize your repo:
    $ git init

  1. Add files to your repo by running the following command:
    $ git add .

This will add all files in the current directory to your repo.

  1. Commit your changes by running the following command:
    $ git commit -m "Commit message"

Replace "Commit message" with a short description of the changes you've made.

  1. Create a new branch by running the following command:
    $ git branch new-branch

Replace "new branch" with the name of your new branch.

  1. Switch to the new branch by running the following command:
    $ git checkout new-branch
  1. Make changes to your files and commit them to the new branch.

  2. Merge the new branch into the main branch by running the following command:
    $ git checkout main
    $ git merge new-branch

macOS

Git is preinstalled on macOS, so you can use Git from the command line without installing anything else.

  1. Open a terminal window by searching for "Terminal" in Spotlight or opening it from the Applications/Utilities folder.

  2. Navigate to the directory where you want to initialize your repo.

  3. Run the following command to initialize your repo:

    $ git init

  1. Add files to your repo by running the following command:

    $ git add .

This will add all files in the current directory to your repo.

  1. Commit your changes by running the following command:
    $ git commit -m "Commit message"

Replace "Commit message" with a short description of the changes you've made.

  1. Create a new branch by running the following command:
    $ git branch new-branch

Replace "new branch" with the name of your new branch.

  1. Switch to the new branch by running the following command:
    $ git checkout new-branch
  1. Make changes to your files and commit them to the new branch.

  2. Merge the new branch into the main branch by running the following command:
    $ git checkout main
    $ git merge new-branch

Linux

Git is typically preinstalled on most Linux distributions. If Git isn't already installed on your system, you can install it using your distribution's package manager. For example, on Ubuntu, you can install Git by running the following command:
$ sudo apt-get install git

Once Git is installed, you can use it from the command line.

  1. Open a terminal window.

  2. Navigate to the directory where you want to initialize your repo.

  3. Run the following command to initialize your repo:
    $ git init

  1. Add files to your repo by running the following command:
    $ git add.

This will add all files in the current directory to your repo.

  1. Commit your changes by running the following command:
    $ git commit -m "Commit message"

Replace "Commit message" with a short description of the changes you've made.

  1. Create a new branch by running the following command:
    $ git branch new-branch

Replace "new branch" with the name of your new branch.

  1. Switch to the new branch by running the following command:
    $ git checkout new-branch
  1. Make changes to your files and commit them to the new branch.

  2. Merge the new branch into the main branch by running the following command:
    $ git checkout main $ git merge new-branch

for more such commands visit: https://training.github.com/downloads/github-git-cheat-sheet/

GitHub command-line interface

In addition to using Git from the command line, you can also use GitHub from the command line using the GitHub CLI. The GitHub CLI provides an easy way to create and manage repositories, issues, pull requests, and more from the command line.

Windows/macOS

To use the GitHub CLI from the command line on Windows, you'll need to install it first.

  1. Download the GitHub CLI for Windows from the official GitHub website: https://cli.github.com/

  2. Run the installer and follow the instructions.

  3. Open a command prompt or Git Bash terminal and for Mac open the terminal.

  4. Authenticate to GitHub by running the following command and following the prompts:
    $ gh auth login

  1. Create a new repository by running the following command:
    $ gh repo create

Follow the prompts to provide a name and description for your new repository.

  1. Push your changes to GitHub by running the following command:
    $ git push origin main

Linux

To use the GitHub CLI from the command line on Linux, you can install it using your distribution package manager. For example, on Ubuntu, you can install the GitHub CLI by running the following commands:

sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-key C99B11DEB97541F0
sudo apt-add-repository https://cli.github.com/packages
sudo apt-get update
sudo apt-get install gh

other commands CLI will be the same for all
for more details about CLI visit https://docs.github.com/en/github-cli

Thanks for this! ๐Ÿ’– and grab it.. see you in the next blog. Happy coding ๐Ÿš€

ย