Git and GitHub Basics for Beginners
Introduction
This document serves as a beginner's guide to Git and GitHub, providing essential information to get started with version control and collaboration.
What is Git?
Git is a distributed version control system that allows you to track changes in your code over time. It enables multiple developers to work on the same project without interfering with each other's work.
What is GitHub?
GitHub is an online platform that hosts Git repositories. It provides a web-based graphical interface and features like bug tracking, task management, and wikis for projects.
What is git branch?
A branch is a separate version of the project. It is used to develop features isolated from each other. The master/main branch is the default branch when you create a repository. You can create new branches and switch between them to work on different features or versions of a project.
Setting Up Git
Download and Install Git
- Visit Git's official website and download the version suitable for your operating system.
- Follow the installation instructions.
Configure Git
- Open your terminal (Command Prompt or PowerShell in Windows).
- Set your username:
git config --global user.name "Your Name"
- Set your email address:
git config --global user.email "your_email@example.com"
Basic Git Commands
- Initializing a Repository
git init
This command creates a new Git repository in your current directory.
- Cloning a Repository
git clone <repository-url>
This command is used to obtain a repository from an existing URL.
- Adding Files
git add <file>
git add .
Adds a file to the staging area (git add <file>
) or adds all changed files (git add .
).
- Committing Changes
git commit -m "Commit message"
Record changes to the repository with a descriptive message.
- Pushing Changes
git push origin <branch-name>
Sends your commits to the remote repository.
- Pulling Changes
git pull
Fetches and merges changes from the remote server to your working directory.
- Changing Branches
git checkout <branch-name> or git checkout -b <branch-name> # create new branch
Switches to a different branch (version of the project). The -b
flag is used to create a new branch.
- Merging Changes
git merge <branch-name>
This command merges changes from a different branch into your current branch. It is used to integrate changes from another branch into your active branch.
Using GitHub
- Create a GitHub Account
- Visit GitHub and sign up.
- Create a New Repository
- Click the '+' icon in the top right corner and select 'New repository'.
- Fill in the repository details and create it.
- Pushing to GitHub
- After committing your changes locally, push them to GitHub:
git push origin master
- Pull Requests
- Used to inform others about changes you've pushed to a branch in a repository.
- Once a pull request is opened, you can discuss and review the potential changes with collaborators.
- The pull request can be merged into the base branch if the changes are approved.
- Merging Pull Requests
- After reviewing and discussing a pull request, it can be merged into the base branch.
- The merge can be done in the different ways:
- Merge Commit: Creates a new commit for the merge, preserving the commit history.
- Squash Merge: Combines all commits into a single commit (Useful for cleaning up a messy Git history).
- Rebase and Merge: Moves the commits from the feature branch to the base branch, making it appear as if they were written on top of the base branch.
Conclusion
This document provides a basic overview of Git and GitHub. For more detailed information, consult the official Git documentation and the GitHub Help.