Git and GitHub Quick Guide

Guide to Git and GitHub for Beginners

Introduction

Git is a distributed version control system that helps developers manage and track changes in their code. GitHub is a cloud-based hosting service for Git repositories, enabling collaboration among developers. This guide will cover the basics of using Git and GitHub, starting from scratch.

Table of Contents

  1. Installing Git
  2. Setting Up Git
  3. Basic Git Commands
  4. Creating a GitHub Account
  5. Creating a Repository on GitHub
  6. Connecting Local Repository to GitHub
  7. Cloning a Repository
  8. Making Changes and Committing
  9. Pushing Changes to GitHub
  10. Branching and Merging
  11. Pull Requests
  12. Collaborating on GitHub
  13. Handling Merge Conflicts
  14. Advanced Git Commands
  15. Best Practices

1. Installing Git

To start using Git, you need to install it on your machine.

Windows

  1. Download the installer from the Git website.
  2. Run the installer and follow the instructions.

macOS

  1. Install Homebrew if you don't have it: /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
  2. Install Git: brew install git

Linux

  1. Use your package manager to install Git. For example, on Ubuntu: sudo apt-get install git

2. Setting Up Git

Configure Git with your name and email address. This information will be associated with your commits.

git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"

3. Basic Git Commands

Learn some basic Git commands to initialize a repository and perform essential operations.

  • Initialize a new Git repository:
    git init
  • Check the status of your repository:
    git status
  • Add files to the staging area:
    git add <file>
  • Commit changes:
    git commit -m "Commit message"

4. Creating a GitHub Account

  1. Go to GitHub.
  2. Click on "Sign up" and follow the instructions to create your account.

5. Creating a Repository on GitHub

  1. Log in to your GitHub account.
  2. Click the "+" icon in the top right corner and select "New repository".
  3. Fill in the repository name, description (optional), and choose the visibility (public or private).
  4. Click "Create repository".

6. Connecting Local Repository to GitHub

In your local repository, add the GitHub repository as a remote:

git remote add origin https://github.com/yourusername/your-repository.git

If you already have set the remote origin, and you want to change it, then you need to do this:

git remote set-url origin https://github.com/yourusername/your-repository.git

7. Cloning a Repository

To work on an existing repository, clone it to your local machine.

git clone https://github.com/username/repository.git

8. Making Changes and Committing

  1. Make changes to your files.
  2. Add the changes to the staging area:
    git add <file>
  3. Commit the changes:
    git commit -m "Description of the changes"

Commit Message Conventions

To maintain a clean and understandable commit history, follow these conventions:

  • Use the imperative mood in the subject line (e.g., "Fix bug" instead of "Fixed bug" or "Fixes bug").
  • Keep the subject line under 50 characters.
  • Separate the subject from the body with a blank line.
  • Use the body to explain what and why vs. how if necessary.
Add user authentication

- Implement login functionality
- Add user registration page
- Integrate with the authentication API

Fixes #123
Some companies utilize the following categories as a commit message convention:

Commit Types

  • feat: Used when a new feature is developed.
  • fix: Used when a broken functionality is fixed.
  • bugfix: Used when a bug is fixed.
  • hotfix: Used for urgent or minor fixes.
  • chore: Used for tasks not directly related to development, such as maintenance.
  • refactor: Used when restructuring existing code.
  • style: Used for visual adjustments only.
  • test: Used for tasks related to the test suite.

9. Pushing Changes to GitHub

Push your local commits to the GitHub repository.

git push origin main

Replace main with your branch name if different.

10. Branching and Merging

Creating a Branch

Create a new branch to work on a feature or fix a bug, and switch to it.

git checkout -b feature-branch

Listing All Branches

List all branches and see your current branch with * sign.

git branch -a

Showing Your Current Branch

Use this to see your current branch only.

git branch --show-current

Swithcing To Existing Branch

Use this to switch to another branch.

git checkout feature-branch

Deleting Unwatched Files From Current Branch

When you get back to the main branch, you may want to see only the related files. In this situation, you can delete the files of other branches with the following commands. It won't delete them from branches that do track those files.

List all untracked files that will be deleted.

git clean -n

If it seems okay, delete those files.

git clean -f -d

Merging a Branch

Merge your branch into the main branch after completing your work.

  1. Switch to the main branch:
    git checkout main
  2. Merge the feature branch :
    git merge feature-branch

Merging Your Branch With Up-to-date Main

Sometimes, your work with your branch can get a so long time that, it gets absolute comparingly with the main branch. In such cases, to get over possible problematic future conflicts, you should better update your current branch with merging the main branch.

  1. Switch to the main branch:
    git checkout main
  2. Update your main branch:
    git pull
  3. Switch to your branch:
    git checkout your-branch
  4. Merge the up-to-date main branch with your branch:
    git merge main
  5. Now, you can compare and merge the branches if any conflicts occur. Then complete the merge:
    git add .
    git commit -m "Merge main into your-branch-name"

Deleting a Branch

Delete a branch from local and remote.

  1. Delete branch locally:
    git branch -d localBranchName
  2. Delete branch remotely:
    git push origin --delete remoteBranchName

Fetching From Origin With Deleted Remote Branches

When you delete some branches from remote, you will not be able to sync your local with the remote. In such cases, you can solve the problem with the following code.

git fetch -p

Branch Naming Conventions

Using consistent branch naming conventions helps keep your repository organized. Here are some common conventions:

  • feature/branch-name: For new features (e.g., feature/login-page)
  • bugfix/branch-name: For bug fixes (e.g., bugfix/fix-login-error)
  • hotfix/branch-name: For critical hotfixes (e.g., hotfix/patch-security-vulnerability)
  • release/branch-name: For release branches (e.g., release/v1.0.0)

11. Pull Requests

A pull request (PR) allows you to notify others about changes you have made.

  1. Push your branch to GitHub:
    git push origin feature-branch
  2. Go to your GitHub repository, and you will see an option to create a pull request.

12. Collaborating on GitHub

Forking a Repository

  1. Navigate to the repository you want to fork.
  2. Click the "Fork" button in the top right corner.

Submitting Changes

  1. Clone your forked repository.
  2. Make changes and push them to your fork.
  3. Create a pull request from your fork to the original repository.

13. Handling Merge Conflicts

Merge conflicts occur when changes are made to the same part of a file in different branches.

Resolving Conflicts

  1. Identify conflicted files by running:
    git status
  2. Open the files and look for conflict markers (<<<<<<<, =======, >>>>>>>).
  3. Edit the files to resolve conflicts and remove conflict markers.
  4. Add the resolved files to the staging area and commit.

14. Advanced Git Commands

  • Stashing changes:
    git stash
  • Viewing commit history:
    git log
  • Rebasing:
    git rebase

15. Best Practices

  1. Commit often with meaningful messages.
  2. Use branches for new features and bug fixes.
  3. Keep your branches up to date with the main branch.
  4. Review pull requests thoroughly.
  5. Resolve conflicts promptly.

Conclusion

Git and GitHub are powerful tools for version control and collaboration. By following this guide, you can start using Git and GitHub effectively in your development workflow. Practice regularly to become proficient and explore more advanced features as you progress.





Comments