Using Git and GitHub

Last Updated : 19 Jan, 2026

Git and GitHub are important tools for modern software development, enabling version control, collaboration, and efficient code management.

Prerequisites

  • Git Installed: Download and install Git from Git's official website.
  • GitHub Account: Sign up for a GitHub account at GitHub.
  • Configured Git: Set up your Git username and email:
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"

Git

Git is a distributed version control system used to track code changes and support collaborative development.

  • Maintains complete project history with branching and merging support.
  • Enables multiple developers to work on the same codebase efficiently.

GitHub

GitHub is a web-based platform built on Git that enables hosting, collaboration, and project management.

  • Supports collaboration through pull requests, issues, and code reviews
  • Provides automation and CI/CD through GitHub Actions

Working with Git

Here are the basic steps to work with Git for managing and tracking code changes.

Step 1: Install Git

Download and install Git from Git's official website. Follow the instructions for your operating system.

Step 2: Configure Git

Set up your name and email to identify your commits:

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

Step 3. Create a New Repository

To start a new project, create a new directory and initialize it as a Git repository:

mkdir my-project
cd my-project
git init

Step 4. Clone an Existing Repository

To contribute to an existing project, clone a repository from GitHub:

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

Step 5. Track Changes

Add files to your repository and track changes:

touch file.txt
git add file.txt
git commit -m "Add file.txt"

Step 6. View History

To view the history of your commits:

git log

Working with GitHub

Here are the basic steps to work with GitHub for hosting, collaborating on, and managing code repositories.

Step 1. Create a GitHub Repository

  • Log in to your GitHub account.
  • Click the "+" icon in the upper-right corner and select "New repository."
  • Name your repository and choose its visibility (public or private).
  • Click "Create repository."

Step 2. Connect Local Repository to GitHub

Link your local Git repository to the GitHub repository:

git remote add origin https://github.com/username/repository

Step 3. Push Changes to GitHub

To push your local commits to GitHub:

git push -u origin main

Replace 'main' with the name of your main branch if different.

Step 4. Pull Changes from GitHub

To update your local repository with changes from GitHub:

git pull origin main

Step 5. Create a Branch

Branching allows you to work on new features without affecting the main code:

git checkout -b new-branch

Step 6. Push a Branch to GitHub

Push your new branch to GitHub:

git push -u origin new-branch

Step 7. Create a Pull Request

A pull request (PR) is a way to propose changes to a repository. To create a PR:

  • Go to your GitHub repository.
  • Click the "Pull requests" tab.
  • Click "New pull request."
  • Select the branch with your changes and the base branch.
  • Click "Create pull request" and provide a description.

Step 8. Review and Merge a Pull Request

Collaborators can review PRs, suggest changes, and approve them. Once reviewed, you can merge the PR:

  • Go to the PR on GitHub.
  • Click "Merge pull request."
  • Confirm the merge.

Step 9. Handle Merge Conflicts

Merge conflicts occur when changes in different branches clash. To resolve conflicts:

  • Pull the latest changes from the base branch:
git pull origin main
  • Git will mark conflicting files. Open these files and resolve the conflicts.
  • After resolving conflicts, add the resolved files and commit:
git add .
git commit -m "Resolve merge conflicts"

Step 10. Use Issues

GitHub Issues help track bugs, enhancements, or tasks. To create an issue:

  • Go to the "Issues" tab in your GitHub repository.
  • Click "New issue."
  • Provide a title and description, then click "Submit new issue."

Step 11. Use GitHub Actions

GitHub Actions allows you to automate workflows. To set up an action:

  • Go to the "Actions" tab in your repository.
  • Choose a workflow template or create your own.
  • Follow the prompts to configure and save the workflow.

Advanced Git and GitHub Tips

1. Rebase for a Cleaner History

Rebasing integrates changes from one branch into another and provides a linear commit history:

git checkout main
git pull origin main
git checkout your-branch
git rebase main

2. Squash Commits

Squashing combines multiple commits into one, simplifying your commit history:

git rebase -i HEAD~n

Replace n with the number of commits to squash.

3. Use .gitignore

Create a .gitignore file to exclude files from being tracked by Git. Add file patterns to this file:

*.log
*.tmp
node_modules/

4. Stash Changes

If you need to switch branches but have uncommitted changes, use git stash to save them temporarily:

git stash

Retrieve stashed changes later with:

git stash pop

Best Practices

Promotes clean version control, effective collaboration, and a stable codebase.

  • Regular Commits: Commit changes frequently with clear messages.
  • Descriptive Branch Names: Use meaningful names for branches, like feature/authentication or bugfix/issue-42.
  • Code Reviews: Review and test code in pull requests before merging.
  • Use Issues: Track bugs and enhancements through GitHub Issues.
  • Sync Often: Regularly pull changes from the main branch to keep your branch up-to-date.
Comment

Explore