Skip to main content

Introduction to Git & GitHub

Git​

Git is a version control system that allows you to track changes to your code, collaborate with others, and manage your codebase. It helps you keep a record of who made changes, when they were made, and why they were made.

GitHub​

GitHub is a web-based hosting platform designed for developers. It provides a centralized location to store your code repositories and enables collaboration among team members. GitHub supports projects in various programming languages and allows others to review and contribute to your code. It is a free and open-source platform, making it widely used and popular among developers.

Steps to Create an Account on GitHub​

  1. Open the GitHub website by visiting Click Here to Open GitHub Website
  2. Click on the Sign up button.
step-1
  1. Enter your email address.
step-2
  1. Create a strong and secure password.
step-3
  1. Choose a unique username for your GitHub account.
step-4

6.Click the Create an account button.

step-5
  1. Complete the CAPTCHA puzzle to verify that you are a human.
step-6
  1. Check your email for a message from GitHub and click the Verify email address button.
step-7

Repository (Repo)​

In GitHub, a repository, or "repo" for short, is a central location where you can store your source code and related files for a specific project.

Steps to Create Repository​

  1. On the GitHub page, click on the + drop-down menu in the upper-right corner and select New repository.
img-1
  1. Enter a name for your repository, for example, sample.
img-2
  1. Choose the visibility of your repository (private or public).
img-3
tip

It is recommended to keep your repository public.

  1. Click the Create repository button to create your repository.
img-4
  1. You will see an empty repository page.
img-5
  1. Open your Git Bash (command line) and run the following command, replacing <repository-link> with the link available on your repository:
img-6
git clone <repository-link>
img-8
tip

Replace <repository-link> with the actual link available on your repository

  1. You will see a new folder named sample on your desktop.
img-7

Account Setup Credentials​

To set up your account credentials for Git, follow these steps:

  1. Open a terminal or command prompt.

  2. Set your username globally using the command:

    git config --global user.name "Your Username"
  3. Set your email globally using the command:

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

Git Commands​

  1. git init

The git init command is used to initialize a new Git repository in your project folder. This command is used once during the initial setup of a new repository.

  1. git status

The git status command displays the status of your working project. It shows which files have been modified or staged and which files are untracked.

  1. git add <filename>

The git add <filename> command adds new or changed files in your working project to the Git staging area. You can add specific files by providing their filenames, or you can use git add . to add all changed files.

  1. git commit

The git commit command is used to record the changes in the repository. Each commit includes a commit message that describes the changes made in that commit.

Syntax :

git commit -m "commit message"
  1. git push

The git push command is used to upload local branch commits to GitHub. It pushes your local changes to the remote repository on GitHub.

To push your code to GitHub using Git commands, follow these steps:

Pushing Existing Code to a Git Repository​

  1. Initialize a Git repository: If you haven't already done so, navigate to the root directory of your project using a command-line interface, and run the following command to initialize a Git repository:
git init
  1. Create a remote repository: Create a new repository on a Git hosting platform such as GitHub Follow the platform's instructions to set it up.

  2. Add a remote repository: Link your local repository with the remote repository using the following command:

git remote add origin <remote_repository_url>

Replace <remote_repository_url> with the URL of the remote repository you created.

  1. Add files to the staging area: Use the following command to add the existing code files to the staging area, which prepares them for committing:
git add .
  1. Commit the changes: Committing creates a new snapshot of the code in the Git history. Run the following command to commit the changes:
git commit -m "Initial commit"
  1. Push the code: Finally, push your code to the remote repository using the following command:
git push

If you're using a branch other than master, replace master with the name of the branch.

Congratulations! You have successfully pushed your existing code to a Git repository.

Pushing New Code Directly to a Git Repository​

  1. Clone the repository: Start by cloning the existing repository to your local machine. Use the following command to clone the repository:
git clone <repository_url>

Replace <repository_url> with the URL of the repository you want to clone.

  1. Create or modify files: Create new code files or modify existing ones using your preferred code editor or IDE.

  2. Add files to the staging area: Use the git add command to add the new or modified files to the staging area:

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

Replace Commit message with a descriptive message summarizing the changes made.

  1. Push the code: Finally, push the new code to the remote repository using the following command:
git push

Great job! You have now successfully pushed your new code directly to the Git repository.