Mastering Git and GitHub: A Beginner's Guide
Table of contents
This blog post will guide you through the essentials of Git and GitHub, providing you with the knowledge and commands you need to get started.
What is Git?
Git is a distributed version control system that helps you track changes in your code over time. It allows multiple developers to work on the same project simultaneously without conflicting with each other's work.
HASH :- unique identifier for each commit in a Git repository.
UNTRACKED :- Git doesn’t know about them yet—they’re not in any snapshot (commit) or staging area. like newly created files
STAGED :- When you git add
a file, it becomes staged. These are the changes you’ve specifically chosen to include in the next commit.
TRACKED :- When you git commit
, it becomes tracked . They are commited and their history is saved .
HEAD :- HEAD is the current commit you’re working with. When you switch branches, HEAD changes to the tip of that new branch.
What is GitHub?
GitHub is a web-based platform that hosts Git repositories. It provides additional features like pull requests, issue tracking, and project management tools, making it an essential tool for collaborative software development.
File system :- file system provides a hierarchical structure for storing and retrieving files . There’s no built-in way to track changes over time or collaborate with others efficiently.
Git Hands-On Commands
1. Configuring Git
After installation, configure your Git username and email:
git config --global user.name "Vaibhav Jaiswal"
git config --global user.email "vaibhavjaiswal@gmail.com"
2. Creating a Repository
To create a new Git repository, navigate to your project directory and run:
git init
3. Basic Git Commands
Add files to staging area:
git add Day2.txt
To Unstage File :-
git rm --cached Day2.txt
Check status of your repository:
git status
or add all files:
git add .
Commit changes:
git commit -m "Your commit message"
View commit history:
git log
View Git commit history in oneline
git log --oneline
git reset command :-Gitreset is a powerful command that is used to undo local changes to the state of a Git repo.
There are 3 ways to reset in git :-
git reset --soft <commit id> # :- soft reset will uncommit your all the changes from that commit id ( One step behind commit ) git reset --mixed <commit id> # :- mixed reset will put all the files in the untracked area from that commit id ( You have to add it then commit ) git reset --hard <commit id> # :- In this your files permanently deleted ( Danger command )
Working with GitHub
1. Creating a GitHub Account
Go to github.com and sign up for an account if you haven't already.
2. Creating a New Repository on GitHub
Click the '+' icon in the top right corner of GitHub
Select 'New repository'
Fill in the repository name and other details
Click 'Create repository'
3. Connecting Local Repository to GitHub
After creating a repository on GitHub, you can connect your local repository to Github:
- git remote add origin <URL>
git remote add origin https://github.com/Vaibhav871/Devops.git
- git push <remote name > <branch name> :- git push will push tour files from the local repository to the remote repository .
git push origin master
- Github After Push the repo from local to remote repository.
- keep track of where your code is stored
git remote -v
4. Cloning a Repository
To clone an existing repository:
git clone https://github.com/username/repo-name.git
Branching and Merging
Branching allows you to diverge from the main line of development and work on features independently.
Create a new branch:
git branch dev
Switch to a branch:
git checkout dev
Create and switch to a new branch in one command:
git checkout -b feature
Merge a branch into the current branch:
# merge dev to master branch git merge dev
Resources :-
Git Documentation:- https://git-scm.com/book/en/v2/Getting-Started-Installing-Git
Atlassian Resource :- https://www.atlassian.com/git/tutorials
Tutorial :- https://www.youtube.com/watch?v=AT1uxOLsCdk
Thank you for reading! Let's continue learning, growing, and making a positive impact in the tech world together.
Happy coding!
Subscribe to my newsletter
Read articles from Vaibhav Jaiswal directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by