1.

Advanced Git Concepts

Answer»
  • git pull --rebase

Git rebase is used to rewrite commits from one branch to another branch. In order to combine unpublished local changes with the published remote changes, git pull is performed.

With git pull --rebase, the unpublished changes will be again applied on the published changes and no new commit will be added to history.

  • git merge --squash

The squash along with git merge produces the working tree. It indexes in the same way as that of the real merge but discards the merge history.

Command:

git merge --squash origin/main

When to use git merge --squash?


  1. When you have merged main into your branch and resolved conflicts.

  2. When you need to overwrite the original commits.

  • git reflog

The reflog records every change that is made in the repository. Apart from this, if some branch is lost from the repo, the recovery can be done using this command.

Command:

git reflog
  • git revert

Revert simply means to undo the changes. Therefore, it is an undo command in Git. Unlike traditional undo operation, the revert command does not delete any data. git revert is a commit operation, as it undo the specified commit.

Command:

git revert

Options:

  • Revert commit:

This option is used to revert back a commit.

Command:

git revert <commit_id>
  • Edit commit message before reverting commit:

In case, we want to edit the commit message before reverting, -e is used for the same.

Command:

git revert -e <commit_id>
  • git bisect

Git bisect is a git tool used for debugging. Suppose, you have a large codebase and some commit causes a bug, but you are not sure of which commit causes it.

Git bisect goes through all the previous commits and uses binary search to find the bugged commit.

The git bisect command is used to find the bisect position as shown. It bisects (divides) your history between the good and the bad commit range. It then moves through every commit id between this range and at each snapshot it allows you to test the code.

It is applied as follows:



  • git bisect start - Starts the bisect


  • git bisect good v1.0 - Mention the last working commit.


  • git bisect bad- Mentioning that the current commit has a bug.

It will return the commit which causes the bug and one can debug the issue efficiently.

  • git blame

git blame is used to know who/which commit is responsible for the latest changes in the repository. The author/commit of each line is visible through this.

Command:

git blame <file_name>

This command shows the commits which are responsible for changes of all lines of code.

  • git cherry-pick

Choosing a commit from one branch and applying it to another is known as cherry picking in Git. Following are the steps to cherry pick a commit:

  • Visit the branch you want to apply to commit and use the following command:
git switch master
  • Run the following command:
git cherry-pick <commit_id

Git Submodules

Submodules are a tool that allows attaching an external repository inside another repository at a specific path. It allows us to keep a git repository as a subdirectory of another git repository.

Commands:


  • Add git submodule: This takes the git URL as the parameter and clones the pointer repo as a submodule. The syntax to add git submodule is:
git submodule add <URL_link>
  • git submodule init

git submodule init is to copy the mapping from .gitmodules file into ./.git/config file. git submodule init has extend behavior in which it accepts a list of explicit module names.

This enables a workflow of activating only specific submodules that are needed for work on the repository.

Command:

git submodule init

Git Subtrees



  • git subtree lets you nest one repository inside another as a sub-directory. It is one of several ways Git projects can manage project dependencies.

  • git-subtree is a wrapper shell script to facilitate a more natural syntax. This is actually still a part of contrib and not fully integrated into git with the usual man pages.

  • A subtree is just a subdirectory that can be committed to, branched from, and merged along with your project in any way you want.

Commands:


  • add: Let’s assume that you have a local repository that you would like
    to add an external vendor library to. In this case we will add the git-subtree repository as a subdirectory of your already existing git-extensions repository in ~/git-extensions/:
git subtree add --prefix=git-subtree --squash \<Git_repo_link>

  • pull : It is similar to pull from the repository with added prefix.
    Command:
git subtree pull --prefix <URL_link>

Git Submodules VS Subtrees

Git SubmodulesGit Subtrees
It is a link to a commit ref in another repositoryCode is merged in the outer repository’s history
Requires the submodule to be accessible in a server (like GitHub)Git subtree is decentralised, which basically means that its components are shared across a bunch of linked computers.
Git submodule is a better fit for component-based development, where your main project depends on a fixed version of another component (repo).Git subtree is more like a system-based development, where your all repo contains everything at once, and you can modify any part.
Suitable for smaller repository sizeSuitable for bigger repository size



Discussion

No Comment Found