DEV Community

Cover image for Understanding Advance Git
Adil Shahzad
Adil Shahzad

Posted on

Understanding Advance Git

Branches

Git branching allows developers to diverge from the production version of code to fix a bug or add a feature.
Here is an example of how git branches are helpful. Let's say you need to work on a new website feature, create a branch and checkout to the new branch, and start working. Once you complete new features, you can merge your changes to the master branch.

Branch
As shown in the image above, we have a default branch, the master branch. To add new features to the project, we can easily create a new branch and start working; someone else from the team member can also contribute to the Repository by creating a new branch.

How to create a branch

Option 1: Creating a branch & Checkout

You can create your new branch with the following command.

git branch <new_branch>
Enter fullscreen mode Exit fullscreen mode

Replace with whatever name you want.

and with the following command, you can list the branches

git branch
Enter fullscreen mode Exit fullscreen mode

The current local branch will be marked with an asterisk (*).

If the asterisk is not visible on your new branch, it means you are not on the new branch. With the following command, you can easily check out the new branch.

git checkout <new_branch>

Enter fullscreen mode Exit fullscreen mode

Now, you can use the git branch to check which branch you are currently using. The current branch will be marked with an asterisk(*).

Option 2: Creating a branch using checkout

Through this command, you can easily create a new branch and checkout to the branch simultaneously.

git checkout -b <another_branch>

Enter fullscreen mode Exit fullscreen mode

And with the git branch you can check on which branch you are currently. The current branch will be marked with an asterisk(*).

delete a branch

To delete the local branch, use the following command.


git branch -d my-branch-name
Enter fullscreen mode Exit fullscreen mode

The -d option only deletes the branch if it has already been merged. The -D option is a shortcut for --delete --force, which deletes the branch irrespective of its merged status.


git branch -D my-branch-name

Enter fullscreen mode Exit fullscreen mode

merging

Once you complete your work with the branches, you need to use the Merge method. Merging takes your branch changes and merges them to the main branch, the master branch, by default. Depending on the commit history, git performs two ways of merging.

  1. Fast-forward or two-way merging
  2. Three-way merging

Fast-forward

In the Fast Forward method, where merging includes only snapshots, let me clear more with an example. Let's say you have completed your work on the feature branch (which is your new branch), and you merge the changes with the master branch, which is the default branch in git. Assume that the master branch has no more commits from the time you created a new branch.
Fast Forward Method
When the feature branch has to be merged with the master branch, the two of the most recent commits on either of these branches, C3 and F2, compare and merge automatically, unless no merge conflict error occurred—called fast forward or two-way merge.

Three-way merge (or recursive)

The Three-Way process involves three snapshots, two of the snapshots are involved in the two-way process (Fast-Forwarding) as already discussed. The third one is the base file or the Parent, with which two files are compared.

Three Way Method

As you see, C3 is the Parent with which C4 and F2 files are compared for merging.

How merging works

As we have already discussed Merge in detail, let's do some practical hands-on. We have also learned how to create a new branch, the file created inside the new branch will not be accessible to the master branch, which is our default branch in Git, until we merge the new branch with the master branch.

Merging branches in a local repository

Use git checkout to switch the branch you want to merge into to merge locally. The branch we usually switch first is typically the master branch, which is the default branch. Next, use the git merge and specify the branch name to merge the new branch with the master branch. This method is also called Fast Forward Merge.

Create a new branch using the command git branch new branch you can change the new branch name specifically to your project feature, for example, dev branch and you can easily checkout using the git checkout command when you create a new file in the new branch you need to follow the staging and committing procedure. Once the process is completed, you can follow the commands given below;


git checkout master

git merge <newbranch>

Enter fullscreen mode Exit fullscreen mode

Aborting merge

You can also abort the merging process in case of merge conflict issues with the following command.

git merge --abort
Enter fullscreen mode Exit fullscreen mode

Merge only one specific commit

Sometimes we need to merge a specific commit; use cherry-pick. Let's discuss what cherry-pick is.

Cherry-pick

Cherry-pick allows you to choose the specific existing commit to include in another branch, and with the help of cherry-picking, you can merge a particular commit of exiting.

Cherry-Pick

Create a new branch, checkout to the new branch, add the files, stage, and then commit your changes in the new branch. Use the following command to get the specific commit ID.

git log --pretty=format:"%h %s" 
Enter fullscreen mode Exit fullscreen mode

You can easily merge the specific commit ID to the default branch master using commit ID. Once copied, the commit ID is then checked out to the master branch and used the following command.

git checkout master
git cherry-pick 12670fd
Enter fullscreen mode Exit fullscreen mode

Merge Conflict

Merge conflicts may occur if completing changes are made to the same line of a file, or if the file that someone else is attempting to edit is deleted. We will discuss further how to resolve to merge conflict issues using Github.

What is Rebase?

If you are collaboratively working on a project with other developers, then you would notice very quick changes on the repository, and it would not be the same repository you have started to work with, as new features and changes would be added. This is where rebasing comes into action. Rebasing takes commits and stores them in a temporary location. It takes the latest version and adds the commit on top of the latest version. So in this way rebasing ensures that your commits are added to the current state of the project, and not to the old state of the project.

Rebase Vs Merge

Rebasing and rebasing both are designed to integrate change from one branch to another branch but in different ways.

Let's say we are working on a project with a repository having committed. Merge will result as combinations of commits, whereas rebase will add changes in the feature branch starting from the last commit of the master branch.

  1. When you rebase a feature branch onto the master, you move the base of the feature branch to the master branch's ending point.

  2. Merging takes the content of the feature branch and integrates it with the master branch. As the result, only the master branch is changed. The feature branch remains the same.

  3. Merging adds a new commit to your repository history.

How Rebase Works

To Rebase the feature branch onto the master branch, use the following commands:

git checkout feature
git rebase master
Enter fullscreen mode Exit fullscreen mode

Squash

Git squash is a technique that helps you to take a series of commits and consider it to a few commits. for example, Assume that you have a series of commits with the help of git squash we can me all the n number of commits to a single commit.

Squashing is mainly used to describe a large number of commits, to make it to a small number of meaningful commits so that you can make the git history clean.

Git squash is also used for the merging of branches. Most developers also advise you to always squash the commits and rebase them with the parent branch(master).

So when you are squashing and rebasing with the parent branch, the history of the master branch will be way clean, and you will have meaningful commits.

How to do git squash

First, we will use the following command to print the logs in one line.

git log --oneline
Enter fullscreen mode Exit fullscreen mode

Let's squash the last three commits. These commits explain that we have added a new file. We can use the following command to squash the last three commits.

git rebase -i HEAD~3
Enter fullscreen mode Exit fullscreen mode

The Interactive -i command will open up the editor which will have the three commits. Just add the s word to the last commit as shown in the image below and save the file.

In Vim you can Insert by pressing the key i. The command to save a file in Vim and quit the editor is:wq. To save the file and exit the editor simultaneously, press Esc to switch to normal mode, type:wq, and hit Enter.

After writing and quitting your file using vim, the editor will open another editor having the commits, you can change the commits, as shown in the image below. And when you are done with your changes, git squash will merge the commits to one.

You can check whether the last three commits are merged by the git squash using the command below:

git log --oneline
Enter fullscreen mode Exit fullscreen mode

Git stash

According to the Atlassian

The Git Stash command takes your uncommitted changes(both staged and unstaged changes), saves them for later on, and then revert them from your working copy.

It is a way to store any uncommitted changes and revert your file to the last commit. Then you can do any other git command during this time. When ready, you can reapply your stored changes into your working directory.

Why should we use git stash

Imagine the scenario, you are working on a new feature branch for a project. A Developer asks for some help and wants you to check something in another branch. However, the changes you made in the new feature branch are not entirely committed. Here, git stash comes in handy; instead of making half-completed commits, you can temporarily save your uncommitted changes using git stash. Then you can safely check out to another branch. When you are finished helping the developer, you can return to your branch and continue exactly where you left.

How to use git stash

These are the two basic commands which we will use.

Use this command to stash your current work.

git stash

Enter fullscreen mode Exit fullscreen mode

Use this command to reapply your stashed work back to your working area, and with this command, you can remove it from the stash.


git  stash pop

Enter fullscreen mode Exit fullscreen mode

gitignore

.gitignore file is a file that specifies the files or folders that we want to ignore.

  1. Let's say we want to ignore a text file; with the help of the .gitignore file, we can easily ignore the text file. Assume that you created a readme.txt file in my working directory, and you want to ignore the file; write the readme.txt file to the .gitignore file.

  2. And if you want to ignore files using the extensions, write the *.txt to the .gitignore file.

  3. Let's say you want to ignore a folder. Assume that you are working on a python project and usually use the venv environment in python. You can write the venv/ to the .gitignore file to overlook this folder.

How does gitignore work?

From the Git bash terminal, we can easily create the .gitignore file.


touch .gitignore
Enter fullscreen mode Exit fullscreen mode

While using Vim to edit the .gitignore file, you can edit the file from the terminal with this command,

vi .gitignore
Enter fullscreen mode Exit fullscreen mode
  • Press I key to insert text into the .gitignore file
  • to quit :wq to successfully write and leave from the vim editor.

With the git status command, you can check whether the file is ignored or not.

releases & version tags

Tags are a simple aspect of git; they allow you to identify specific release versions of code. You can think of a tag as a branch that does not change. Once you create the tag, it loses the ability to change the history of commits.

There are two types of git tags.

  1. Annotated tags
  2. Lightweight tags

Annotated tags

Annotated tags store metadata such as author name, release notes, tag message, and data stored as an object in the git database. All this data is important for the public release of the project.

To create a tag using the following command.

git tag -a v1.0.0
Enter fullscreen mode Exit fullscreen mode

In case if you want to add a tag message, then use the following command.

git tag -a v1.0.0 -m " Release version v1.0.0"
Enter fullscreen mode Exit fullscreen mode

Lightweight tags

Lightweight tags are the simplest way to add a tag to your git repository because they store only the hash of commit as lightweight tags do not contain extra information.

Lightweight tags are essentially bookmarks to a commit; they are just a name and pointer to commit. - Bitbucket

To create lightweight tags, you can use the following command.

git tag v2.0.0
Enter fullscreen mode Exit fullscreen mode

What have we learned so far?

We learned different advanced concepts of Git, containing branches, merging, cherry-pick, and tags. As these concepts are a bit hard for beginners, but if you focus more on the hands-on git commands, you can easily get the concepts of the git advance features, as we discuss every topic in detail, this is a brief overview of some git advance commands.

  1. To list branches, we use the command git branch and to create the branch, we use git branch new branch and then switch to the new branch we use the command git checkout new branch.

  2. To merge the new branch with the default branch, first, we need to switch back to the default branch with the following command git checkout master and then we can merge the new branch to the default branch with the following command git merge new branch.

  3. To merge a specific commit, we learn about the cherrypick, with the following command, we can merge a specific commit git cherrypick <commit-ID>

Please feel free to use the terminal for the git commands so that you can learn and have hands-on experience with the git advance commands. Please follow up as I already added the working on every git command.

Summary

This lesson mainly dealt with the advanced git concepts; as we covered branches and why we use branches, we learned advanced concepts of Git. In the next lesson, we will learn about the Git remote repositories, why we use git remote repositories, and How to learn about a handy tool: GitHub.

If you have any question, please feel free to connect with me on LinkedIn.

Top comments (0)