Git is a specific open-source version control system created by Linus Torvalds in 2005.

Specifically, Git is a distributed version control system, which means that the entire codebase and history are available on every developer’s computer, which allows for easy branching and merging.

GitHub is a for-profit company that offers a cloud-based Git repository hosting service. Essentially, it makes it a lot easier for individuals and teams to use Git for version control and collaboration.


GitHub’s interface is user-friendly enough so even novice coders can take advantage of Git. Without GitHub, using Git generally requires a bit more technical savvy and the use of the command line.


GitHub is so user-friendly, though, that some people even use GitHub to manage other types of projects.


Additionally, anyone can sign up and host a public code repository for free, which makes GitHub especially popular with open-source projects.


As a company, GitHub makes money by selling hosted private code repositories, as well as other business-focused plans that make it easier for organizations to manage team members and security. We utilize Github extensively to manage the projects of our clients.


For using Git you have to know commands which will help you to easily work with Git from your terminals,


Initialize a local repository


git init <directory>


Set configuration value for your username and email:


git config --global user.name <your name> 

git config --global user.email <your email>


Clone a repository


git clone <repository-url>


Staging files


git add <file_name>

// Stage a Single file


git add <file_name1> <file_name2>

// Stage multiple files


git add *.js 

//Stage a pattern


git add . 

// Stages the current directory and all its content


Viewing the status


git status

// Check full status


git status -s

// Check short status


Committing the staged files


git commit -m “Message” 

// Commit with one line message


git commit

// Opens the default editor to type a long message


Skipping the staging area


git commit -am “Message”


Removing files


git rm <file_name>

// Removes from working directory and staging area 


git rm --cached <file_name>

// Removes from staging area only


Renaming or moving files


git mv <old_file_name> <new_file_name>


Viewing the staged/unstaged changes


git diff 

// Shows unstaged changes


git diff --staged

// Shows staged changes


git diff --cached

// Shows staged changes(same as above)


Viewing the history


git log 

// Shows Full history


git log --oneline 

// Shows summary


git log --reverse

// Shows the list of commits from the oldest to the newest


git log --stat 

// Shows the list of modified files


git log --patch

// Shows the actual changes (patches)


Filtering the history


git log -3 

// Shows the last 3 entries


git log --author=“Rahul”

// Shows entries from author Rahul


git log --before=“2022-08-17”

// Shows entries before 2022-08-17


git log --after=“one week ago”

// Shows entries after one week ago


git log --grep=“task”

// Show commits with “task” in their message


git log -S“task”

// Show commits with “task” in their patches  


git log hash1..hash2

// Show entries between range of commits


git log <file_name>

// Show commit that touched file


Formatting the log output


git log --pretty=format:”%an committed %H”


Creating an alias


git config --global alias.lg “log --oneline"



Viewing a commit


git show <commit_id>

// Shows the given commit


git show HEAD

// Shows the last commit


git show HEAD~2

// Shows two steps before the last commit 


git show HEAD:<file_name>

// Shows the version of <file_name> stored in the last commit


git show HEAD~2:<file_name>

// Shows the version of file stored in this commit 


Comparing commits


git diff HEAD~2 HEAD 

// Shows the changes between two commits 


git diff HEAD~2 HEAD file.txt 

// Changes to file.txt only


Unstaging files


git restore --staged <file_name>

// Copies the last version of file from repo to index


Discarding local changes


git restore <file_name>

// Copies file from index to working directory


git restore <file_name1> <file_name2>

// Restores multiple files in working directory


git restore .

// Discards all local changes (except untracked files) 


git clean -fd 

// Removes all untracked files


Restoring an earlier version of a file


git restore --source=HEAD~2 <file_name>


Checking out a commit


git checkout <commit_id>

// Checks out the given commit


git checkout master

// Checks out the master branch


Finding a bad commit


git bisect start

// Start binary search session


git bisect bad 

//Marks the current commit as a bad commit


git bisect good <commit_id>

// Marks the given commit as a good commit 


git bisect reset 

// Terminates the bisect session


Finding contributors


git shortlog


Viewing the history of a file


git log file.txt 

// Shows the commits that touched file.txt


git log --stat file.txt 

// Shows statistics (the number of changes) for file.txt


git log --patch file.txt 

// Shows the patches (changes) applied to file.txt


Finding the author of lines


git blame <file_name>

// Shows the author of each line in file


Tagging


git tag v1.0 

// Tags the last commit as v1.0 


git tag v1.0 <commit_id>

// Tags an earlier commit


git tag

// Lists all the tags


git tag -d v1.0

// Deletes the given tag


Managing branches


git branch <branch_name>

// Creates a new branch


git checkout <branch_name>

// Switches to the branch


git switch <branch_name>

// Switches to the branch


git switch -C <branch_name>

// Create and switch to the branch


git branch -d <branch_name>

// Deletes the branch


git branch -D <branch_name>

//  Force delete the branch


Comparing branches


git log master..<branch_name>

// Lists the commits in the branch not in master


git diff master..<branch_name>

// Shows the summary of changes


Stashing


git stash push -m “New stash”

// Creates a new stash 


git stash list 

// Lists all the stashes


git stash show stash@{1}

// Shows the given stash


git stash show 1 

// Shortcut for stash@{1}

 

git stash apply 1

// Applies the given stash to the working dir


git stash drop 1 

// Deletes the given stash


git stash clear 

// Deletes all the stashes


Merging


git merge <branch_name>

// Merges the branch into the current branch 


git merge --no-ff <branch_name>

// Creates a merge commit even if FF is possible


git merge --squash <branch_name>

// Performs a squash merge


git merge --abort 

// Aborts the merge


Viewing the merged branches


git branch --merged 

// Shows the merged branches


git branch --no-merged 

// Shows the unmerged branches


Rebasing


git rebase master 

// Changes the base of the current branch


Cherry picking


git cherry-pick <commit_id>

// Applies the given commit on the current branch


Syncing with remotes


git fetch origin master 

// Fetches master from origin 


git fetch origin 

// Fetches all objects from origin


git fetch

// Shortcut for “git fetch origin”


git pull 

// Fetch + merge


git push origin master 

// Pushes master to origin


git push 

// Shortcut for “git push origin master”


Sharing tags


git push origin v1.0 

// Pushes tag v1.0 to origin 


git push origin —delete v1.0

// Pushes tag v1.0 to origin and delete that tag


Sharing branches


git branch -r 

// Shows remote tracking branches


git branch -vv 

// Shows local & remote tracking branches


git push -u origin <branch_name>

// Pushes branch to origin


git push -d origin <branch_name> 

// Removes branch from origin


Managing remotes


git remote

// Shows remote repos


git remote add upstream url 

// Adds a new remote called upstream 


git remote rm upstream 

// Remove upstream branch


Undoing commits


git reset --soft HEAD^ 

// Removes the last commit, keeps changes staged


git reset --mixed HEAD^ 

// Unstages the changes as well


git reset --hard HEAD^ 

// Discards local changes


Reverting commits


git revert <commit_id>

// Reverts the given commit


git revert HEAD~3.. 

// Reverts the last three commits


git revert --no-commit HEAD~3..

// Reverts the last three commits without creating commit


Recovering lost commits


git reflog 

// Shows the history of HEAD


git reflog show <branch_name>

// Shows the history of branch pointer


Amending the last commit


git commit --amend


I hope you will be going to use these commands, if you know some more commands let us know in the comments.