Git main commands easy explanation 😃✨

Git main commands easy explanation 😃✨

Hello Friends 👋 Some may say there is no need to learn git well I don't know what to say about them but I think it's very helpful to reduce the complexity of projects and reviewing them...

Every time I use git it saves a lot of time ⏲ and whenever I make a mistake 😬 it can easily be saved 😎


Git

It is a free and open-source distributed version control system designed to handle everything from small to very large projects with speed and efficiency.😕
😂🤣 In layman words Git help user to push their code and files in Github and also makes collaboration easier, allowing changes by multiple people to all to be merged into one source.


As on that note, some basic and main Git commands are 👇, and command with <text> should be replaced with user data/branch/path/name/value.

Git Init💡

It is the first Git command 🎉 unless it's already used and Git inits initialized the folder to a Git repository(repo).
This will transform the current directory into a Git repository.

$ git init


Git status💡

It is one of the most used git commands as it provides all information you need like
• Your current branch
• Is there anything to be pushed, pulled, or committed
• If you have created any new file or modified or deleted

$ git status


Git Add💡

Every time you create a new file or modify it or delete it you have to call the git add command to track the file otherwise it wouldn't be added when you push the file.

$ git add <file-name>

But this ☝ will only add a single file, for adding all the files use 👇

$ git add -A
//Or
$ git add .


Git Commit💡

It is used to save the changes you make with a message to briefly describe the changes you made to your future self or other team member's to quickly understand 🧐 what change you did✔
⚠ Commit command only saves the changes in your local repository

I remember everything 😏😏 -- By No one

To commit without message used 👇

$ git commit -a

Ohh!😮 You are still here and now you don't remember what changes you made day's ago 😑 Therefore it is recommended to use a message for sake of human memory 🧠

$ git commit -a -m "<commit-message>"

Git Push💡

To make all of your commits pushed into the GitHub repository and to be available for your teammates you have to push them to the remote origin.
A remote in Git is a common repository that all team members use to exchange their changes. In most cases, such a remote repository is stored on a code hosting service like GitHub or on an internal server. Remote eg. origin

$ git push <remote> <branch-name>

🤪You need to be more serious 😥 while using git push and branch name is also important here Otherwise, you may change/disturbed the main code which is in the master/main(default) branch.


Git Branch💡

This is one of the most important functionalities that Git provides. It allows working on the code parallelly. If you are working on n number of features by creating n number of separate branches will be most helpful, efficient, and safe.

To create a new branch use 👇
$ git branch <branch-name>

Now you have created a local branch ☝, it can be used for your use. If you want this to be visible to all members you have to push the branch

$ git push  -u <remote> <branch-name>
Command to view all branches
$ git branch
//OR
$ git branch -list
Command to delete a branch
$ git branch -d <branch-name>


Git Checkout💡

After making a branch you need to change to that branch also. For that purpose, there is another command
🛑Stop before changing branch and check whether the current brunch is committed or not by using git status and then push them in the repository by git push <remote> <branch-name>

$ git checkout <branch-name>

Steps to pull a branch from the repo are discussed later... ⌛
Ohh!!! 😨so many things to remember, but no problem there is a shortcut for creating a branch and changing to it

$ git checkout -b <branch-name>


Git Pull💡

With the pull command, you can download different branches in the local system It also helps you to fetch all the changes of your teammates pushed and automatically merge them into your local repo.

$ git pull <remote>

If you changed some code added by a teammate it can easily be solved by

Ask them and resolve the issue... 😶


Git Diff💡

As the name suggests it show's the difference in working branch and asked branch

$ git diff

git diff will show the difference between the last commit and current modification in the local repo of the same branch

$ git diff <branch1>..<branch2>

This will compare the two stated branches and show differences between them.

$ git diff <branch1> <branch2> <.path>
// Path of the file should be accurate.


Git Clone💡

Git clone is a command to clone a repository. Instead of download or zipping a repository using git clone

$ git clone <url-of-repository>

The code and files will automatically download to your local machine


Git Stash💡

As the name suggests it store/save 📋 something safely. This is useful when you are in the middle of your work code is uncommitted and can switch to another branch and come back later. 🛑This will only stash tracked ✔ files i.e. all files added by git add a

$ git stash save "<staged-message>"
//This will help when you will come back and restore your stash

For stashing untracked ❌ files use

$ git stash save -u

For viewing all stashed files use

$ git stash list

//Eg:-
//stash@{0}: On master: Stashed with message1
//stash@{1}: On master: Stashed with message2

The number inside the curly brace shows the id of stashes if you want to restore a particular it can be easily 🎉 done with help of its id.

$ git stash apply

This will restore the newest stash i.e. topmost stash of the stack When you use git stash apply the stashed version will be applied to your current working branch. However, it will not delete the stash from the stack.

$ git stash apply stash@{<n>}

This will restore the specific stash To delete 🗑️ a particular stash use

$ git stash pop stash@{<n>}


Git Log💡

This will display the entire commit history if it's large press [space] to scroll and [q] to quit. The message is written in the commit command i.e. git commit -a -m "<message>"will be more useful now.

$ git log

If you want to see the last n commit use git log -n <number_of_commit_to_see>and this can be more useful if you want to check the last n commit by a unique person 🧔, use:

$ git log -n <number_of_commit_to_see>  --author=<name>


Git Merge💡

Once you are done with your feature branch and tested code you can merge your branch 🧒 with the parent branch 🧔.

$ git merge <branch_to_be_merged>
//You should be in the parent branch at this stage.

Let say you are working on a feature1 branch and the main branch is product1 then 1 Switch to product1 branch

$ git checkout product1

Before merging check your product1 branch is updated by pulling, use git pull 2 After everything is planned and settle merge the feature1 branch

git merge feature1

Last but not least

Git Documentation include more deep information about Git.

My GitHub Profile - AnmolVerma404

My Twitter - @AnmolVe97231707

My LinkedIn Profile - Anmol Verma