You and your team are working on a project, each contributing new features, fixing bugs, and enhancing functionality. How do you ensure that everyone stays on the same page, integrating changes without causing chaos? This is where the magic of Git unfolds. let’s learn more about them.
If you’re looking for a quick reference or need to brush up on the basics, consider checking out Getting familiar with Git
Git Pull
used to fetch and download content from a remote repository and immediately update the local repository to match that content
Simply, Git pull = Git fetch + Git merge
git pull <remote>
Git fetch
Git fetch is a great way to stand at a place from where you can see the changes and decide if you want to keep them or discard them. Does not change your local repository
git fetch <remote>
Fetch all of the branches from the repository. This also downloads all of the required commits and files from the other repository
git fetch --all
Fetches all the registered remotes, files, etc.
git fetch --dry-run
Dry run option will show the user how the command will execute without making any changes
Git Merge
The extraction of the changes done on the remote happens via fetch command and then if the user approves, they are merged with the local repository
used to combine changes from different branches and basic steps:
- Select the target branch
- start the merge
git checkout target_branch
git merge source_branch
- Resolve the conflict (if any)
- Complete the merge
- Commit Message
- Finish the Merge
# Switch to the target branch
git checkout target_branch
# Merge changes from the source branch
git merge source_branch
A fast-forward merge occurs when the branch being merged has no new commits since the branching point. In this case, Git can simply “fast-forward” the branch pointer to the latest commit of the source branch. This results in a linear Git history without a new merge commit.
whereas the — no-ff (no fast-forward) option is used with git merge to ensure that a merge commit is always created, even when a fast-forward merge is possible. This helps preserve a more explicit history and indicates that a feature branch was developed separately before being merged. New merging commit on active branch.
#--no-ff
git checkout target_branch
git merge --no-ff source_branch
#--no-commit
git checkout target_branch
git merge --no-commit source_branch
and then there is — no-commit option prevents Git from automatically creating a new commit during the merge process. It leaves the changes staged but uncommitted, allowing you to review and modify them before finalizing the merge.
Git rebase
Copies the commit from the current branch and commits on the top of specified branch
Rebasing is great whenever you are working on feature branch and the master branch has been updated. You can get all the updates on your branch which would prevent future margin conflict.
git rebase <base>
This automatically rebases the current branch onto <base>, which can be any kind of commit reference
Interactive rebase
- reword — change the commit message
- edit —use commit, but stop for amending
- squash — meld commit into previous commit
- fixup — meld commit into the previous commit without keeping log
- exec — run a command on each commit we want to rebase
- drop — remove the commit
Git reset
A git reset gets rid of all the current staged files and gives us control over where HEAD should print to
- soft reset — moves HEAD to specified commit without getting rid of the changes that were introduced on the commits afterward
- hard reset — moves HEAD and gets rid of changes that were introduced on the commits afterward
git reset --soft
git reset --hard
Git Reverting
we create a new commit that contains the reverted changes
git revert <commit ID>
Git Cherry-pick
when a certain branch contains a commit that introduced changes we need on our active branch, we can cherry pick that command
git cherry-pick <commit ID>
pick the changes that commit does not entire branch
Git reflog
In order to show a log of all the actions that have been taken
git reflog
Disclaimer
I am currently in the process of learning and exploring new concepts, including the topics discussed in this content. While I strive to provide accurate and helpful information, please be aware that I might not be an expert in these subjects. Any explanations or guidance offered are based on my current understanding and learning journey.