Collaboration in Git

Merging

Having multiple branches is extremely convenient to keep new changes separated and avoid pushing unapproved or broken changes to production. When the changes are approved, integrating them into the production branch is essential.

Fast-forward (--ff)

A fast-forward merge occurs when the current branch has no additional commits compared to the branch being merged. Git prefers this as the easiest option: it merges the commit(s) from the branch being merged into the current branch without creating a new commit.

Fast-forward Merge

No-fast-forward (--no-ff)

In case the current branch has commits not present in the branch to be merged, Git performs a no-fast-forward merge. This creates a new merging commit on the active branch, integrating changes from both branches.

No-fast-forward Merge

Merge Conflicts

Merging branches with conflicting changes in the same lines or with deleted files that were modified elsewhere can cause merge conflicts. Git highlights these conflicts, enabling manual resolution by removing unwanted changes, saving, re-adding the changed file, and committing the changes.

Merge Conflicts

Rebasing

Git rebase copies commits from the current branch and places them on top of the specified branch. Unlike merging, rebasing doesn't attempt to decide which files to keep, maintaining the branch's latest changes and avoiding merging conflicts.

Rebasing

Rebasing is ideal when integrating updates from the master branch into a feature branch, preventing future merging conflicts.

Interactive Rebase

Interactive rebase allows modification of commits before rebasing, offering six actions:

  • reword: Edit the commit message.
  • edit: Amend the commit.
  • squash: Combine the commit into the previous one.
  • fixup: Merge the commit into the previous one without retaining the commit's log message.
  • exec: Execute a command on each commit being rebased.
  • drop: Remove the commit.

Interactive Rebase

Feel free to use these actions for full control over your commits, whether removing or merging them. Interactive rebase facilitates the modification of commits.

Soft Reset

A soft reset shifts HEAD to the specified commit without discarding changes introduced by the following commits. It preserves access to the changes made in the previous commits, enabling fixing and recommitting them later.

Soft Reset

Hard Reset

A hard reset returns Git's state to the specified commit, including changes in the working directory and staged files, discarding all changes introduced after the commit.

Hard Reset

Reverting

Git revert creates a new commit that undoes the changes introduced by the specified commit, ensuring the commit history remains unaltered.

Reverting

Git revert is useful for undoing a specific commit without altering the branch's history.