Friday, July 29, 2022

Git Strategies for developers

Git is a version control system or VCS that is primarily employed for source code management and also for keeping track of changes made to a file or a set of files. It enables streamlining the individual processes of multiple developers working on the same project.

What makes a Good Commit?

Rule1- Keep your commits small
Rule2- Keep related changes only in a commit
Rule3- Write meaningful commit messages. 

Try using Conventional Commit Messages - Developers working on real projects in teams, should prefer using conventional commit message standard for consistency and tracking. 


Share Git Stash with another Dev or machine

Create a patch

The following git command will create a patch file that contains all the differences represented by the set of changes in the stash.


git stash show "stash@{0}" -p > changes.patch


The “stash@{0}” is the ref of the stash.
If you want a different one just use $ git stash list to see your list of stashes and select which one you want to patch.

Apply Patch

Transfer/Send the patch file generated from one machine to the other and drop it into the root of your project directory.

Then the following command will do all the magic!!! 

git apply changes.patch
// Reverse patch
git apply changes.patch --reverse


KEEP ON HACKING!!!

The Git Merge Command

The git merge command takes different lines of development created by the git branch and combines them into a single branch. 

The Fast Forward Merge and the 3-way Merge

fast-forward merge is when the Git moves the branch pointer to point at the incoming commit rather than constructing a merge commit. Although this typically happens when performing a git pull command without any local changes, it can also happen for a git merge command.

Because all of the commits reachable from the target branch are now available through the current branch, the fast forward merge combines the histories of the two branches. A fast-forward merge, however, isn’t possible when the branches have diverged.

Git combines two branches using the 3-way merge when there isn’t a linear path to the target branch. It uses a dedicated commit to tie the two histories together. It is called so because, in the 3-way merge, Git uses three commits to generate the merge commit; two branch tips and their common ancestor.

Typically, the fast forward merge is used by developers for small features or bug fixes while the 3-way merge is reserved for integrating longer-running features.

git merge vs. git merge --no-ff

Usually, developers leverage the git merge command to merge two branches. The git command merges (combines) two or more commits into a single history. It’s general syntax is:

git merge <branch>

By default, the git merge command is a fast-forward merge. A fast-forward merge is possible when there is a linear path from the current branch tip to the target branch.

In this scenario, rather than actually merging the two branches, Git integrates the histories, i.e., previous commits, that are to move the current branch tip up to the target branch tip.

For scenarios that require a merge commit during a fast forward merge, we use the Git merge command with the --no-ff flag. The general syntax for this git command is:

git merge --no-ff <branch>

The Git merge --no-ff command merges the specified branch into the command in the current branch and ensures performing a merge commit even when it is a fast-forward merge. It helps in record-keeping of all performed merge commands in the concerning git repo.

Using the --no-ff parameter prevents the Git merge command from performing a fast-forward merge when Git detects that the current HEAD is an ancestor of the commit that you’re trying to merge.

No Fast-forward Merge

At most times, the fast forward merge is preferred. Sometimes, however, there might be a requirement to prevent the fast-forward merge from happening. Typically, this is when maintaining a definite branch topology.

For doing so, the --no-ff parameter can be passed with the git merge command. Resultantly, the git merge command will construct a commit merge rather than fast-forwarding.

What if the git merge can’t fast-forward?

What if you wish to perform the Git merge command for explicitly performing a fast-forward merge and need to withdraw if it can’t fast-forward? Is there some way to do so? Yes, there is.

For doing so, instead of using the --no-ff parameter, you must use the --ff-only parameter. In such a scenario, you can go back and decide whether to merge or rebase if some error occurs during the process.

How to Merge More Than 2 Branches?

The git merge command provides support for a range of merging strategies. For merging more than two branches, two strategies are available:

1. Octopus

This Git merging strategy is mainly used for merging topic branch heads. It is the default merge strategy while pulling or merging more than a single branch. Hence, this is the strategy used when merging two branches.

Although the octopus merge strategy is suitable for resolving cases with more than two heads, it can’t be used for performing a complex merge requiring manual resolution.

2. Ours

Ours merge strategy resolves any number of heads. The resulting tree of the merge, however, belongs to that of the current branch head and ignores all changes from the remaining branches. It is typically used to supplant the old development history of the side branches.

Conclusion

By now, you must be clear with the difference between the git merge and Git merge --no-ff commands. Understanding the difference is important to put both the git commands to the appropriate use.


Ref: https://hackr.io/blog/difference-between-git-merge-and-git-merge-no-ff#:~:text=It%20is%20called%20so%20because,for%20integrating%20longer%2Drunning%20features.

No comments:

Post a Comment

Cybersecurity Essential: OpenSSL

In today’s digital landscape, securing data is paramount. Whether you’re building web applications, managing servers, or developing software...