DEV Community

Back2Basics
Back2Basics

Posted on

Git Back to Basics

Git is Open Source Version Control System. It is a distributed VC system.In this blog I will guide you git basics and interview preparation.

Check the Version of git installed

# if not found then install via following command
sudo apt-get install git
git --version
Enter fullscreen mode Exit fullscreen mode

Create Local Repository

git init
Enter fullscreen mode Exit fullscreen mode

Git merge types:

  1. Fast-Farward merge
  2. No-Fast-Farward merge

Fast-Farward merge:

In this merge strategy the master branch has not commits after the checkout to feature branch. So, git allows the Fast-Farward merge. It does not create extra commit for merging.

Image description

No-Fast-Farward merge

No-Fast-Farward merge only allows when the master branch has some commits after the feature branch checkout. To merge the feature branch commits to master branch the master branch will create new commit after merging the changes from feature branch.

Image description

To View the commit history on a branch checkout to the branch and run

git log
# for last commit
git log -n 1
Enter fullscreen mode Exit fullscreen mode

Lets say to work on the new git repo we need to clone it. It will dont need to initialize the git local repo after cloning the ".git" directory will be created. We can add the changes to local repo then push changes to remote repo.

git clone <git-repo-url>
Enter fullscreen mode Exit fullscreen mode

To pull the latest changes after cloning the repo some passed away. We need latest changes to resolve merge conflicts. So run the git pull it will do the fetching + merging changes to the local repo.

# git pull <remote-repo-alias> <branch>
git pull origin master
Enter fullscreen mode Exit fullscreen mode

Lets say only need to fetch the changes and want to see the latest changes in remote repo then merge the change to the local master branch.

# git fetch <remote-repo-alias> <branch>
git fetch origin master
# to view the latest changes in the remote repo 
# git log -n <index>
git log -n 1 # for last commit
# git merge <remote-repo-alias>/<branch> <loal-master-branch>
git merge origin/master master
Enter fullscreen mode Exit fullscreen mode

Image description

Git Rebase

Rebase works by copying the commit history of one branch to other branch. With rebase we can squash the multiple commits into one commit.

# checkout from master to feature branch run
git rebase master
# for squashing/changing the multiple commits into one commit
git rebase -i
# leave the first commit as it is and rest of the commits which you want to remove and add those commit changes to the first commit add the "squash" replacing the "pick" keyword.
# save the file and add the commit message then save, exit.
Enter fullscreen mode Exit fullscreen mode

Cherry-pick

There is a commit on specific file with some modifications on master branch. We want to file only to work on the feature branch try the cherry-pick by taking the hash-id from master branch.

# on master branch
git log  --oneline --name-only
git checkout feature-branch
git cherry-pick <hash-id>
# if there were merge conflits fix manually.
Enter fullscreen mode Exit fullscreen mode

Git Revert/Reset

There is some file or changes adding in a commit. We want to revert it.Identify the commit hashid revert it.For this git creates new commit for reverting the changes.

git revert <hash-id>
Enter fullscreen mode Exit fullscreen mode

Image description

To have the access to the reverted changes later user --soft flag

git revert --soft HEAD~1 # last commit reverting
# check the changes are under radar of git
git status
Enter fullscreen mode Exit fullscreen mode

With the --hard flag the commit changes reset with out saving those changes. Try with above commands changing the flag --soft to --hard

Git stash

There is some changes under staging area we dont want them under staging for some time. So move them to stash area.

git stash
Enter fullscreen mode Exit fullscreen mode

To bring back to staging area

git pop
Enter fullscreen mode Exit fullscreen mode

To see the stash list

git stash list
Enter fullscreen mode Exit fullscreen mode

To see specific stash changes

git stash show stash@{1}
Enter fullscreen mode Exit fullscreen mode

To get the specific stash back to staging area

git stash pop stash@{1}
Enter fullscreen mode Exit fullscreen mode

If there is some hard reset mistakenly and want to get it back.

Image description

Git working areas:

Image description

Top comments (0)