Learn Git


What is git?
What problem did it solve?
Imagine you have a team of 20 developers, all working on the same project. Keeping track of all the changes made by each developer can be very difficult and complicated. That's where our saviour, Git, comes in.
Git is a free, open-source distributed version control system (DVCS) that tracks changes in files, making it easy to revert to previous versions, collaborate with others, and manage different versions of a project's code. It is widely used in software development and other fields that require tracking changes over time.
Install Git
GitHub for Windows:- htps://windows.github.com
GitHub for Mac:- htps://mac.github.com
For Linux and Solaris platforms, the latest release is available on the official Git web site.
Git for All Platforms:- htp://git-scm.com
Getting started with Git
git config --global user.name “[firstname lastname]” #set a name that is identifiable for credit when review version history
git config --global user.email “[valid-email]” #set an email address that will be associated with each history marker
git config --global color.ui auto #set automatic command line coloring for Git for easy reviewing
Initialise Git and clone repo
git init #initialises git which track changes
git clone [URL] #clone required repo into local system
Track your changes and the status of the file
git status #show modified files in working directory, staged for your next commit
git add [file] #add a file as it looks now to your next commit you can use . to all files
git reset [file] #unstage a file while retaining the changes in working directory
git diff #diff of what is changed but not staged
git diff --staged #diff of what is staged but not yet commited
git commit -m “[descriptive message]” #commit your staged content as a new commit snapshot
Create a branch and merge
git branch [branch name]
git checkout -b [branch name] #this will create branch and checkout
git checkout [branch name]
git merge [branch] #merge the specified branch’s history into the current one
Check Logs
git log #show the commit history for the currently active branch
git log branchB..branchA #show the commits on branchA that are not on branchB
git log --follow [file] #show the commits that changed file, even across renames
git diff branchB...branchA #show the diff of what is in branchA that is not in branchB
git show [SHA] #show any object in Git in human-readable format
Update your local with a remote code
git remote add [alias] [url] #add a git URL as an alias
git fetch [alias] #fetch down all the branches from that Git remote
git merge [alias]/[branch] #merge a remote branch into your current branch to bring it up to date
git push [alias] [branch] #Transmit local branch commits to the remote repository branch
git pull #fetch and merge any commits from the tracking remote branch
Rebase
git rebase [branch] #apply any commits of current branch ahead of specified one
git reset --hard [commit] #clear staging area, rewrite working tree from specified commit
Stash
Stash is used to save your changes so you can use them later.
git stash #Save modified and staged changes
git stash list #list stack-order of stashed file changes
git stash pop #write working from top of stash stack
git stash drop #discard the changes from top of stash stack
How to avoid tracking specific files?
To avoid tracking specific files, create a .gitignore
file and write the paths of the files or directories you want to ignore.
Subscribe to my newsletter
Read articles from Amit Gupta directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
