DEV Community

Cover image for Git & GitHub: A Beginner's Guide
SyntaxStories
SyntaxStories

Posted on

Git & GitHub: A Beginner's Guide

Git is a version control system (VCS), a tool that helps track changes in code over time. Version control systems allow you to track the history of your code, including when changes were made, what changes were made, and by whom. It’s essential for collaboration on projects because it enables multiple developers to work on the same codebase without interfering with each other’s work.

GitHub, on the other hand, is a platform that allows developers to store and manage their code repositories online, using Git. It provides a web-based interface for interacting with Git repositories and helps in sharing, collaborating, and versioning code across multiple developers.

Git Basics

1. Commit:

  • To commit in Git is to finalize changes. Whenever you make a change in your project, you commit it to save those changes in Git’s memory.
  • You can commit changes by clicking on "commit changes" to save those changes in memory.

2. README:

  • A README file provides important information about your project. It's optional but highly recommended.
  • It is not just a basic text file; it follows some predefined syntaxes to make it more readable. For example, you can use basic HTML tags to format the content. To insert a line break, you would use the <br></br> tag.

3. Setting Up Git:

  • To use Git on your machine, you need to install Git Bash on Windows and Terminal on Mac.
  • To check if Git is already installed, run the following command in the command prompt: Image description

Configuring Git

  • To tell Git which account to use for making changes, you need to configure your user details.

Syntax to configure:

Image description

  • To check what configurations you’ve made so far, use the command:

Image description

  • There are two types of configurations:

1. Global Level: These settings apply to the entire system and affect all Git repositories.
2. Local Level: These settings are specific to a project or repository and use a different email ID or username.

  • You can run these commands in Git Bash (Windows), Terminal (Mac), or the integrated terminal in Visual Studio Code (VSCode).

Cloning a Repository and Checking Status

1. Clone:

  • Cloning means copying a repository from GitHub to your local machine. You can do this by using the following command:

Image description

  • To clone a repository, go to the GitHub repository you want to clone, click on the green "Code" button at the top, and copy the URL provided. Then, run the git clone command with the copied URL.

2. CD (Change Directory):

  • The cd command is used to navigate to different directories.

  • If you want to move to a folder, use:

Image description

  • To move out of any directory, use:

Image description

3. Clear:

  • To clear the terminal, just type clear and press Enter.

4. Checking the Current Directory:

  • To see which folder you are currently in, check the terminal prompt. For example:
  1. On Mac: MacBook-Air CSBasics-demo indicates you're inside the CSBasics-demo folder.
  2. On Windows: C:\Users\YourName\CSBasics-demo> indicates you're inside the CSBasics-demo folder.

5. Listing Files:

  • To see all files in a directory, use:
    Image description

  • This will show visible files. Hidden files can be viewed using ls -a

Git Status

The git status command helps you check the current state of your project in Git. It tells you if there are any changes that need your attention, like uncommitted edits or new files, and whether your branch is synced with the remote repository.

Examples of what git status might show:

  • If everything is up to date:
    Image description

  • This means your local branch matches the latest version of the remote branch.

  • If you’ve made changes but haven’t committed
    them:
    Image description

  • This means you've modified some files, but they’re not ready to be committed yet.

  • If you’ve created new files but haven’t added
    them to Git:
    Image description

  • This means you have new files in your folder, but Git isn’t tracking them yet. You need to add them using git add.

Adding and Committing Changes

1. Add:

  • The git add command is used to add files to the staging area, making them ready for committing.
  • To add a single file: Image description
  • To add all files at once: Image description

2. Commit:

  • The git commit command is used to record changes. After staging files, commit them using:

Image description

  • Commit messages should be meaningful, like "Add new button" or "Fix bug in login form".

3. Push:

  • After committing, the next step is to push the changes to the remote GitHub repository. Use:
    Image description

  • Here, origin is the name of the remote repository, and main is the branch you are pushing to. In case of a different branch, replace main with the branch name.

Initializing a Git Repository

To create a new Git repository for a new project:

  1. Create a new directory using mkdir <directory_name> .
  2. Change into the directory with cd <directory_name> .
  3. Initialize a Git repository using: git init
  4. After initializing, Git creates a hidden .git folder. This folder tracks changes made to your project.
  5. Now, you can add a remote repository (like GitHub) to this project:

Image description

Working with Branches

Branches allow multiple developers to work on different features or fixes simultaneously, without interfering with each other.

1. View Branches:

  • To see which branch you're on, run: Image description

2. Create and Rename Branches:

  • To create a new branch:
    Image description

  • To rename a branch:

Image description

3. Switching Between Branches:

  • To switch to an existing branch: Image description

4. Delete a Branch:

  • To delete a branch: Image description

Merging Code

When working with multiple branches, you can merge them to combine changes from one branch into another.

Merge:

  • To merge another branch into your current branch:
    Image description

  • Before merging, you might want to check the differences between branches: git diff

2. Pull Request (PR):

  • A Pull Request is a way to propose changes to the main project. It’s commonly used in collaborative projects where developers want to merge their code into the main branch.
  • After creating a PR, a senior developer will review it and decide whether to merge the changes or not.

Resolving Merge Conflicts

Sometimes Git cannot automatically merge changes if there are conflicting modifications. For example, if two branches made changes to the same line in a file, Git will raise a conflict, and you must resolve it manually.

Git will highlight the conflicting areas in the file, and you will have to decide which version of the code to keep.

Undoing Changes

1. Undo Staged Changes:

  • If you've staged a file but haven’t committed it yet and want to undo it, use: Image description

2. Undo Committed Changes:

  • If you’ve already committed but want to undo the last commit, use: Image description

3. Undo Multiple Commits:

  • Every commit has a unique hash. To undo multiple commits, use the hash from the git log: Image description

4. Undo All Changes (Local and Remote):

  • To reset both your local and remote repository, use: Image description

Forking Repositories

Forking allows you to make a personal copy of someone else's repository. You can make changes and later propose them via a Pull Request.

Top comments (0)