Git & GitHub for Beginners

Introduction to Git

Git is a distributed version control system that helps developers track changes in their code, collaborate with others, and manage different versions of their projects. It's an essential tool for modern software development, especially when working in teams or on open-source projects.

Setting Up Git

Before you start using Git, you need to install it and configure your identity:

  1. Linux users can install Git using their package manager (e.g., sudo apt install git)
  2. Windows users can download git using choco install git.
  3. Mac users can install Git using brew install git
  4. Open a terminal or command prompt
  5. Set your name and email (used for commit messages):
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"

Creating a repository in GitHub

GitHub Tutorial Link

Github Creating Repo

Basic Git Commands

Initializing a Repository

To start tracking a project with Git, navigate to your project directory and run:

git init

This creates a new Git repository in your current directory.

Checking Repository Status

To see the current state of your repository:

git status

This command shows which files have been modified, staged, or are untracked.

Adding Files to the Staging Area

To prepare files for commit, you need to add them to the staging area:

# Add a specific file
git add filename.txt

# Add all files in the current directory
git add .

# Add all files with a specific extension
git add *.js

Committing Changes

To save your staged changes to the repository:

git commit -m "Your commit message here"

For a more detailed commit message, omit the -m flag:

git commit

This will open your default text editor where you can write a multi-line commit message.

Viewing Commit History

To see the history of commits:

# View all commits
git log

# View a condensed version of the log
git log --oneline

# View the last 5 commits
git log -n 5

Working with Remote Repositories (GitHub)

Cloning a Repository

To create a local copy of a remote repository:

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

Adding a Remote Repository

If you've created a local repository and want to link it to a GitHub repository:

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

Pushing Changes to GitHub

To send your local commits to the remote repository:

# First time push (sets up tracking)
git push -u origin main

# Subsequent pushes
git push

Pulling Changes from GitHub

To fetch and merge changes from the remote repository:

git pull

This is equivalent to running git fetch followed by git merge.

Branching

Branches allow you to work on different features or versions of your project simultaneously.

Creating a New Branch

git branch new-feature

Switching to a Branch

git checkout new-feature

Or, create and switch to a new branch in one command:

git checkout -b another-feature

Merging Branches

To merge changes from one branch into another:

# First, switch to the branch you want to merge into
git checkout main

# Then merge the feature branch
git merge new-feature

Handling Merge Conflicts

Sometimes Git can't automatically merge changes, and you'll need to resolve conflicts manually:

  1. Open the conflicting file(s) in your text editor
  2. Look for the conflict markers (<<<<<<<, =======, >>>>>>>)
  3. Edit the file to resolve the conflict
  4. Save the file
  5. Stage the resolved file: git add filename.txt
  6. Complete the merge by committing: git commit -m "Resolved merge conflict"

Undoing Changes

Discard Changes in Working Directory

git checkout -- filename.txt

Unstage a File

git reset HEAD filename.txt

Amend the Last Commit

git commit --amend

This opens your editor to modify the commit message. If you've staged changes, they'll be added to the amended commit.

Best Practices

  1. Commit often: Make small, focused commits that are easy to understand and review.
  2. Write clear commit messages: Briefly describe what changes were made and why.
  3. Use branches for new features or experiments.
  4. Pull changes from the remote repository before starting new work to avoid conflicts.
  5. Review your changes before committing: Use git diff to see what you've modified.

Online Resources for Further Exploration: