Introduction to Git and GitHub
Git is a distributed version control system that allows developers to track changes in their codebase and collaborate with others effectively. It was created by Linus Torvalds in 2005 and has since become the standard for version control in the software development industry. Git is designed to be fast, flexible, and scalable, making it suitable for projects of all sizes.
GitHub, on the other hand, is a web-based platform that provides hosting for Git repositories. It adds a layer of functionality on top of Git, making it easier to manage and collaborate on projects. GitHub offers features like issue tracking, pull requests, and team collaboration, making it a popular choice for open-source projects and team-based development.
Git is a Version Control System
- Version control is a system that records changes to a file or set of files over time so that you can recall specific versions later.
- Git is a version control system that allows you to track changes to your code and collaborate with others.
Git vs GitHub
| Feature | Git | GitHub |
|---|---|---|
| Purpose | Tool to control and track versions of code. | Cloud service to host git repositories. |
| Installation | Installed and maintained locally on a developer's computer. | Remote connection provider and maintained on cloud/web. |
| Interface | Generally uses command line interface (CLI) with a limited GUI. | Provides a graphical user interface (GUI) to interact with git repositories. |
| Branching and Merging | Allows for branching and merging of code. | Offers pull requests and code review features for collaboration. |
| Basic Features | Provides basic features like committing changes and creating branches. | Offers additional features like issue tracking, project management, and code hosting. |
Git Setup and Configuration
Before diving into the world of Git and GitHub, you need to set up Git on your machine. Start by installing Git from the official website https://git-scm.com and configuring your global settings, including your name and email address. This information will be associated with your commits.
-
Download and install Git from https://git-scm.com/downloads
-
Open Git Bash and check the version of Git installed:
git --version -
Configure Git with your name and email address:
git config --global user.name "Your Name" git config --global user.email "Your Email"
Demo Repository Setup
-
Create a New Folder on your Desktop called
yourFolderName: -
Open Git Bash and navigate to the folder you just created:
cd yourFolderName -
**Initializing a Git Repository:
**To start using Git for version control, you need to initialize a repository. Open your terminal, navigate to the desired project directory, and run the command git init. This command creates a hidden .git directory that will store all the necessary Git data for your project.git init -
If you did not set up global configuration, you can configure Git with your name and email address:
git config user.name "Azraf Al Monzim" git config user.email "your@email.here"You can also check the configuration:
git config --listCheck Global Email and Name:
git config user.email git config user.name -
Create some files in the folder:
touch file1.txt touch file2.txt -
Check the status of the repository:
git statusLetβs add the new file to the staging Area
Staging Area Concept in Git
Basic Git Workflow:
The fundamental Git workflow consists of three stages: the working directory, the staging area, and the repository. The working directory represents the current state of your project, where you make changes to files. The staging area allows you to select specific changes to be included in the next commit, and the repository is where all the committed snapshots of your project are stored.
To track changes in your project, use the command git add <filename> to add specific files or git add . to add all changes. Then, use git commit -m "commit message" to commit the changes to the repository. Remember to write descriptive commit messages that summarize the changes made.
Branching and Merging:
One of the most powerful features of Git is its ability to handle branching and merging with ease. Branches allow you to create separate lines of development, making it possible to work on different features or bug fixes simultaneously. To create a new branch, use the command git branch <branch-name>, and to switch to a branch, use
git checkout <branch-name>When you want to integrate changes from one branch into another, you can use the git merge command. This combines the changes from the source branch into the destination branch. Alternatively, pull requests on GitHub provide a visual interface for reviewing and merging changes between branches. They allow collaborators to discuss changes, suggest modifications, and ensure that the code meets the project's standards before merging.
Collaborating with GitHub:
GitHub provides a platform for effective collaboration among developers. To collaborate on GitHub, start by creating a repository and pushing your local repository to GitHub using git remote add origin <repository-url> and git push -u origin <branch-name>. This establishes a connection between your local repository and the repository on GitHub.
Collaborators can clone the repository, make changes, and submit pull requests for code review. Pull requests provide a centralized location for reviewing code changes, leaving comments, and discussing potential improvements. Project maintainers can review the changes, suggest modifications, and, once approved, merge the changes into the main branch. GitHub also offers additional features like issue tracking, wiki pages, and project management tools, further enhancing collaboration within a team.
Add and Commit [Local Repo]
Adding to the Staging Area:
-
Only a specific File:
git add <file_name> -
Only files of a folder but not the subfolder:
git add . # or git add * -
All files and subfolders recursively:
git add -A -
Only a particular type of file from the directory:
git add *.c -
Only a particular type of file from the directory & subdirectory:
git add **/*.c
Checking the Differences between the previous and current versions:
git diffRestore the earlier version of the file:
git restore < file_name>Removing from Staging Area:
git restore --staged <file_name>Committing to the Local Repository:
-
Only Commit:
git commit -m "Your Commit Message" -
Commit and Add:
git commit -am "Your Commit Message" -
Commit and Add with Editor:
git commit -a -
See Commit History:
git logor for a short version:
git log --onelineor for a short version with graph:
git log --oneline --graph -
See Commit History with Author:
git log --oneline --graph --all --decorate --author="Azraf Al Monzim" -
See Specific Commit:
git show <commit_id>
Undoing from Local Repository:
-
Local Repo to Staging Area:
git restore --staged <file_name> -
Local Repo to Staging Area (Multiple Commit):
git reset --soft HEAD~<number_of_commit>git reset --soft HEAD~2 -
Local Repo to Working Directory:
git restore <file_name> -
Total Deletion of a Commit:
git reset --hard HEAD^
Checkout
-
To a specific commit:
git checkout <commit_id> -
To a specific branch:
git checkout <branch_name> -
To a specific tag:
git checkout <tag_name> -
Move back to the previous branch:
git checkout -
Branching
-
Create a new branch:
git branch <branch_name> -
List all branches:
git branch -
Switch to a branch:
git checkout <branch_name> -
Create a new branch and switch to it:
git checkout -b <branch_name> -
Delete a branch:
git branch -d <branch_name> -
Merge a branch:
git merge <branch_name> -
Merge a branch with a commit message:
git merge <branch_name> -m "Your Commit Message"
Git Collaboration
-
Create a new repository on GitHub
-
Copy the remote repository URL
-
Add the remote repository URL to your local repository:
git remote add origin <remote_repository_URL> -
Clone a Remote Repository:
git clone <remote_repository_URL> -
Push to Remote Repository:
git push -u origin -
Pull from Remote Repository:
git pull -
Fetch from Remote Repository:
git fetch -
Push setting the branch upstream:
git push --set-upstream origin <branch_name>
Essential Git Tips:
- Regularly commit your changes: Committing frequently allows you to capture incremental snapshots of your work, making it easier to track and revert changes if needed.
- Write meaningful commit messages: A well-written commit message provides context and facilitates understanding when reviewing the project's history.
- Use branches for new features or bug fixes: Branching allows for parallel development and isolates changes, keeping the main branch clean and stable.
- Regularly pull changes from the main branch: Keeping your branch up to date with the latest developments in the main branch helps avoid conflicts and ensures a smooth merging process.
- Leverage the power of Git aliases: Git aliases allow you to create shortcuts for frequently used commands, saving time and reducing typing errors.
- Explore Git's extensive documentation and online resources: Git has a rich ecosystem of documentation, tutorials, and online communities that can help you expand your knowledge and discover advanced features.
Git and GitHub are indispensable tools for modern software development. By understanding the basics of version control, branching, merging, and collaborating on GitHub, you can enhance your productivity and streamline your development workflow. Remember to practice regularly, explore advanced Git features, and leverage the collaboration capabilities of GitHub to become a proficient Git user.

