DEV Community

Tracy Chinedu
Tracy Chinedu

Posted on • Edited on

Mastering Git and GitHub: A Complete Guide

Git and GitHub are essential tools in modern software development, allowing for effective version control and collaboration. This blog will guide you through the process of setting up Git on Windows, creating repositories, making commits, and using GitHub for various operations. Whether you're new to Git or just need a refresher, this guide covers everything you need to get started.

What is Git?

Git is a distributed version control system designed to manage source code changes efficiently. It tracks modifications, supports branching and merging, and facilitates collaboration among multiple developers.

What is GitHub?

GitHub is a web-based platform that hosts Git repositories. It provides tools for code review, issue tracking, project management, and more, making it a hub for collaborative development.

Setting Up Git on Windows

1. Install Git

Step 1: Download Git

  1. Visit the Git for Windows website.
  2. The download should start automatically. If not, click the link to manually download the installer.

Step 2: Run the Installer

  1. Double-click the downloaded .exe file to start the installation.
  2. Follow the installation prompts. Here are some key options to consider:

    • Select Destination Location: Choose the default location or specify a custom path.
    • Select Components: By default, Git will install Git Bash and Git GUI, which are both useful. Ensure these options are checked.
    • Choosing the default editor used by Git: You can select your preferred text editor (e.g., Vim, Nano, or Visual Studio Code).
    • Adjusting your PATH environment: Choose "Git from the command line and also from 3rd-party software" to make Git available system-wide.
    • Configuring the line ending conversions: The default option "Checkout Windows-style, commit Unix-style line endings" is usually appropriate.
    • Configuring the terminal emulator to use with Git Bash: The default option is usually fine.
  3. Complete the installation and open Git Bash from the Start menu or desktop shortcut.

2. Configure Git

Open Git Bash and set your global username and email. These details are used for commit messages.

git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
Enter fullscreen mode Exit fullscreen mode

Verify your configuration with:

git config --list
Enter fullscreen mode Exit fullscreen mode

Creating a Repository

A Git repository is where your project's version history is stored. You can create a repository locally or on GitHub.

1. Initialize a Local Repository

  1. Navigate to your project directory in Git Bash:
   cd path/to/your/project
Enter fullscreen mode Exit fullscreen mode
  1. Initialize a new Git repository:
   git init
Enter fullscreen mode Exit fullscreen mode

This creates a .git directory in your project folder, which contains all the Git metadata.

2. Create a Repository on GitHub

  1. Log in to your GitHub account at github.com.
  2. Click the “+” icon in the top right corner and select “New repository.”
  3. Fill in the repository name and description.
  4. Choose to make the repository either public or private.
  5. Click “Create repository.”

You will see instructions for pushing an existing repository or creating a new one. Follow these instructions to link your local repository to GitHub.

Making Commits

Commits are snapshots of your project at a given time.

1. Add Files to Staging

Add files to the staging area to prepare them for commit. To add all files:

git add .
Enter fullscreen mode Exit fullscreen mode

To add specific files:

git add filename
Enter fullscreen mode Exit fullscreen mode

2. Commit Changes

Commit the staged changes with a descriptive message:

git commit -m "Your commit message"
Enter fullscreen mode Exit fullscreen mode

3. View Commit History

To view the commit history, use:

git log
Enter fullscreen mode Exit fullscreen mode

For a simplified view, use:

git log --oneline
Enter fullscreen mode Exit fullscreen mode

Pushing and Pulling Changes

1. Link Local Repository to GitHub

If you initialized your repository locally and need to link it to GitHub, use:

git remote add origin https://github.com/username/repository.git
Enter fullscreen mode Exit fullscreen mode

Replace username with your GitHub username and repository with the name of your GitHub repository.

2. Push Changes

Push your local commits to GitHub:

git push -u origin master
Enter fullscreen mode Exit fullscreen mode

The -u flag sets the upstream branch, so future pushes can be done with just git push.

3. Pull Changes

To fetch and merge changes from GitHub into your local repository:

git pull origin master
Enter fullscreen mode Exit fullscreen mode

Branching and Merging

Branches allow you to work on different features or fixes without affecting the main codebase.

1. Create a New Branch

To create and switch to a new branch:

git checkout -b new-branch
Enter fullscreen mode Exit fullscreen mode

2. Switch Branches

To switch to an existing branch:

git checkout branch-name
Enter fullscreen mode Exit fullscreen mode

3. Merge Branches

To merge changes from one branch into another, first switch to the branch you want to merge into, then:

git merge branch-name
Enter fullscreen mode Exit fullscreen mode

4. Resolve Merge Conflicts

If there are conflicts, Git will mark them in the affected files. Open the files, resolve the conflicts, and then:

git add resolved-file
git commit
Enter fullscreen mode Exit fullscreen mode

Collaborating on GitHub

1. Forking Repositories

To contribute to a repository you don’t own, fork it:

  1. Navigate to the repository on GitHub.
  2. Click the “Fork” button at the top right.

2. Creating a Pull Request

To propose changes to a repository:

  1. Push your changes to your forked repository.
  2. Navigate to the original repository on GitHub and click on “Pull Requests.”
  3. Click “New Pull Request,” select your branch, and click “Create Pull Request.”
  4. Add a title and description, then click “Create Pull Request.”

3. Reviewing Pull Requests

To review a pull request:

  1. Go to the “Pull Requests” tab in the repository.
  2. Click on a pull request to view the changes.
  3. You can comment on specific lines, request changes, or approve the pull request.
  4. Once reviewed, you can merge it into the main branch.

Advanced Git Commands

1. Stashing Changes

To temporarily save changes without committing:

git stash
Enter fullscreen mode Exit fullscreen mode

To apply stashed changes later:

git stash apply
Enter fullscreen mode Exit fullscreen mode

2. Rebase

Rebasing allows you to apply commits from one branch onto another base:

git rebase branch-name
Enter fullscreen mode Exit fullscreen mode

Be cautious with rebasing, as it rewrites commit history.

3. Cherry-Pick

Cherry-picking allows you to apply a specific commit from one branch to another:

git cherry-pick commit-id
Enter fullscreen mode Exit fullscreen mode

Conclusion

Git and GitHub are powerful tools for version control and collaboration. With this guide, you’ve learned how to set up Git on Windows, create repositories, make commits, push and pull changes, and use advanced features. By mastering these basics, you’ll be well-equipped to manage your code effectively and collaborate with others.

For further exploration, delve into Git’s advanced features and integrate GitHub with other tools to optimize your development workflow. Happy coding!

Top comments (0)