Git me some knowledge (Merge, Rebase and Amend)
Author: Sunil Banmala
In this article, let's explore the essence of Git: when to smoothly merge for collaboration, create a clean history with rebasing, and add those final touches with amending. Join us in the dance where code meets simplicity!
Git Merge
Git merge combines sequences of commits of changes from one branch to the target branch. The git merge command in Git is used to combine changes from one branch into another. It allows you to integrate the changes made in one branch (usually called the "source" branch) into another?branch (usually called the "target" branch). This is a fundamental operation in Git when working with branches and collaborating on code with others. Here's the basic syntax:
git merge <source-branch>
Steps of git merge:
1. All the changes on source branch (branch with new changes which are to be merged into target branch) are all committed using commands:
git add <files to be committed>
git commit -m "message"???
2. Now switch to target branch to which the changes from source branch is to be reflected.
?????git checkout <target-branch>
3. Then merge the source branch into the target branch.
????git merge <source-branch>
Git will automatically merge the source branch into the target branch if there are no conflicts on the code.?
If there are conflicts, Git will pause the merge process and inform you about the conflicts. You'll need to manually resolve these conflicts by editing the affected files, removing conflict markers, and then committing the resolved changes. After resolving all conflicts, you can continue the merge by running git merge --continue or git commit to complete the merge.
Once all conflicts are resolved, and you complete the merge, Git will create a new merge commit, combining the changes from the source branch into the target branch.
Finally, you can push the changes to a remote repository if you're collaborating with others, using git push.
Git Rebase
Rebasing is the process of moving or combining a sequence of commits from source branch to a new base commit. It allows us to modify the commit history by moving, combining, or removing commits. Rebase can help maintain a clean and linear history by incorporating the changes from one branch onto another. It is suitable for feature branches or when you want to keep the commit history tidy. Rebase simply pull the commits from target branch and rearrange the commits as per required. Here's the basic syntax:
git rebase target-branch
Steps of git rebase:
1. All the changes on source branch (branch with new changes which are to be merged into target branch) are all committed using commands:
????git add <files to be committed>
????git commit -m "message"?
2. Now use the git rebase command to rebase the source branch onto the target branch:
领英推荐
?????git rebase <target-branch>????
Here all the new commits of source branch apply each commit from the source branch on top of the target branch one by one.
3. Resolve Conflicts (if any): Just like in merging, conflicts may arise during the rebase process. Resolve these conflicts as they appear.
4. Then checkout to target branch:
??? git checkout <target-branch>
5. Then merge the source branch into the target branch.
????git merge <source-branch>
Finally, you can push the changes to a remote repository if you're collaborating with others, using git push.
When to git merge and git rebase?
The choice between merging and rebasing depends on your workflow and the project's requirements:
Use Merging When:
Use Rebasing When:
Git Amend
Git amend is used to amend new changes on last commit. There are situations when minor changes are to be made after committing a new feature on the branch. In such condition,?git commit --amend is there for us. This command simply adds the new staged changes to last commit and gives new commit id to last commit. Syntax:
git add <files to be committed>
git commit --amend
git push
Conclusion
Rebasing, merging and amend are essential tools in the Git?that help development workflow. Understanding when to use each method and following the steps outlined in this guide will enable you to manage your codebase efficiently and collaborate effectively with your team.
Read more: Git me some knowledge!