16th Feb 2024
u/monzim
Git and GitHub are powerful tools for version control and collaborative software development. This comprehensive guide will walk you through the basics of using Git and GitHub, explain how they work together, and provide essential tips to enhance your workflow.
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 | GitHub |
---|---|
Tool to control and track versions of code. | Cloud service to host git repositories. |
Installed and maintained locally on a developer's computer. | Remote connection provider and maintained on cloud/web. |
Generally uses command line interface (CLI) with a limited GUI. | Provides a graphical user interface (GUI) to interact with git repositories. |
Allows for branching and merging of code. | Offers pull requests and code review features for collaboration. |
Provides basic features like committing changes and creating branches. | Offers additional features like issue tracking, project management, and code hosting. |
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.
git --version
git config --global user.name "Your Name"
git config --global user.email "Your Email"
yourFolderName:
cd yourFolderName
git init
git config user.name "Azraf Al Monzim"
git config user.email "your@email.here"
You can also check the configuration:
git config --list
Check Global Email and Name:
git config user.email
git config user.name
touch file1.txt touch file2.txt
git status
Let’s add the new file to the staging Area
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.
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.
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.
git add <file_name>
git add .
# or
git add *
git add -A
git add *.c
git add **/*.c
Checking the Differences between the previous and current versions:
git diff
Restore the earlier version of the file:
git restore < file_name>
Removing from Staging Area:
git restore --staged <file_name>
git commit -m "Your Commit Message"
git commit -am "Your Commit Message"
git commit -a
git log
or for a short version:git log --oneline
or for a short version with graph:git log --oneline --graph
git log --oneline --graph --all --decorate --author="Azraf Al Monzim"
git show <commit_id>
git restore --staged <file_name>
git reset --soft HEAD~<number_of_commit>
e.g.
git reset --soft HEAD~2
git restore <file_name>
git reset --hard HEAD^
git checkout <commit_id>
git checkout <branch_name>
git checkout <tag_name>
git checkout -
git branch <branch_name>
git branch
git checkout <branch_name>
git checkout -b <branch_name>
git branch -d <branch_name>
git merge <branch_name>
git merge <branch_name> -m "Your Commit Message"
git remote add origin <remote_repository_URL>
git clone <remote_repository_URL>
git push -u origin
git pull
git fetch
git push --set-upstream origin <branch_name>
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.