Git & Git Hub Advance

Git & Git Hub Advance

DevOps Blog: 3

Let's hand on Git advanced commands that are very useful as a DevOps Engineer. For this, firstly understand the concept of Git branching.

Branches in Git

In industries, there is not a single person in a department who is taking care of everything, but there is a team in every department that is working in collaboration. Is it possible to manage everyone's work without affecting the main repository? The answer is yes, with the help of branching. Branching is the process of making a new or separate version of the main repository that works independently of the main repository. The one working with a branch can add, delete, modify or commit anything without affecting the main repository.

git checkout -b <new_branch_name> To switch to a new branch

git checkout <existing_branch_name> To switch to an existing branch

Git Pull: It is the command used to sync the local repository by fetching and merging the updates from the remote repository. It downloads the content from the remote repository and updates the local repository to match the content.

git pull origin main To pull from a remote repository

Git Push: It is the command used to update all the commits you did in the local repository to the remote repository. It uploads the commits from the local repository to update the remote repository.

git push origin main To push to a remote repository

Git Merge: It is the command to merge independent lines of development created by git branch and integrate them into a single branch.

Current branch: the branch to be merged.

Target branch: the branch in which we want to merge the current branch.

git checkout <target_branch>

git merge <current_branch>

To do so, first, check out in to target branch to merge the current branch and then perform the merge. This will merge the current branch into the target branch.

Git Restore: This command is used to restore the file from the working tree that has been deleted. A file can be restored before committing only. Once the commit is done, the restore command does not work. So, what if one wants to go to a previous commit; to do so there is another command as git revert.

git restore <file_name>

Git Revert: This command is used to undo the last commit done. It is to be noted that git revert does not delete any file, it only takes you to the previous commit and lets you add it as a new commit. The last commit can be adding, modifying or deleting a file as shown below.

git revert <commit_id>

Git Reset: As we know, git revert is used to cancel the last commit. But what if you want to keep your progress up to a particular commit and discard all other commits after that particular commit? To do so, we use reset. A reset can be soft or hard based on the need of your system. Usually, a soft reset is performed. As shown in the above screenshot, after revert, the log will be generated but after reset, there is no such log generated. Moreover, the content of the file remains the same but the file will be in the working area. So, you can modify the file and commit it as a new commit.

git reset --soft <commit id>

Resolving Conflicts in Git

There is a situation while working with a local repository, the sync to the remote repository breaks. This gives rise to conflict. Git gets confused about which directory's progress should be considered and which one should be ignored. This conflict can occur when there a file is being written by Team A as well as by team B simultaneously, and Team B commits before Team A. Now, when Team A will perform a commit, it has to face an error that there is some work in the remote repository that Team A does not have. To resolve this issue, Team A has to merge both the changes done by Team B and itself to generate a new commit. This can be done with the help of rebase.

Git Rebase: In Git, the term rebase is referred to as the process of moving or combining a sequence of commits to a new base commit as shown in the screenshots below.

As shown in the screenshot above, the conflict raises as the file gets updated in the remote repository. This conflict will be resolved by rebase.

git pull origin <branch_name> --rebase

The file below represents the rebase output where Head points to the latest commit from remote and the code below represents the local commit. Now, its up to you which code you want to carry forward.

Hence, the conflict was resolved.

Cherry-pick

With the "cherry-pick" command, Git allows you to integrate selected, individual commits from any branch into your current HEAD branch. Contrast this with the way commit integration usually works in Git: when performing a Merge or Rebase, all commits from one branch are integrated. Cherry-pick, on the other hand, allows you to select individual commits for integration.

To do so, Find the commit hash that needs to be cherry-picked first. Go to the destination branch. Resolve the conflicts if they happen and perform a new commit as shown in the screenshots below.

git cherry-pick <hash_id>

Difference between Merge and Rebase

Both git merge and git rebase are designed to integrate the changes of one branch into another but in different ways. For example, merging the dev branch to the main branch will only update the main branch with the latest commit of dev branch whereas, dev branch remains the same. While performing rebase, both the branches dev as well as main will get updated with all the commits performed in both branches as shown in screenshots.

Git Squash

In Git, squashing is used to squash the previous commits into one. It makes it easy to keep track of all the commits by squashing them into one rather than managing every single commit. It is not a command; instead, it is a keyword. The squash is an excellent technique for group-specific changes before forwarding them to others. You can merge several commits into a single commit with the compelling interactive rebase command.

As shown above, four commits of the staging file are merged into one.

Hope you find this article interesting and insightful. Thanks for reading. Happy learning. - Neha Bhardwaj