Merging and Resolving Conflicts via CLI - bounswe/bounswe2024group11 GitHub Wiki

Merging and Resolving Conflicts via CLI

You can access the general git tutorial here

Merging Branches

Follow these steps to merge branches using the command line.

1. Fetch and Pull Latest Changes

Before merging, ensure your local repository is up-to-date:

git fetch origin
git pull origin target-branch

Replace target-branch with the branch you're merging into (e.g., main).

2. Attempt the Merge

for squash and merge, refer How to Squash and Merge in Git below. Merge the source branch (e.g., feature-branch) into the target branch:

git checkout target-branch
git merge feature-branch

If conflicts occur, Git will stop the merge and display messages like:

CONFLICT (content): Merge conflict in <file>
Automatic merge failed; fix conflicts and then commit the result.

Resolving Conflicts

If conflicts arise during a merge, follow these steps to resolve them.

1. Identify Conflicts

List all files with conflicts:

git status

Files with conflicts will be marked as both modified.

2. Open and Resolve Conflicts

Open each conflicted file in your preferred text editor (e.g., VSCode, Vim, Nano). Locate conflict markers:

<<<<<<< HEAD
// Changes from the current branch
=======
// Changes from the merging branch
>>>>>>> feature-branch

Decide which changes to keep, modify, or combine. After resolving the conflict, remove the conflict markers (<<<<<<<, =======, and >>>>>>>).

3. Mark the File as Resolved

After resolving conflicts in a file, mark it as resolved:

git add <file>

Repeat this step for each conflicted file.

4. Complete the Merge

Once all conflicts are resolved and staged:

git commit

Git may pre-fill a merge commit message. Edit it if necessary, or accept the default message.

Additional Commands and Tips

Push Changes

After resolving the merge, push the changes to the remote repository:

git push origin target-branch

Abort the Merge

If you want to cancel the merge and return to the previous state:

git merge --abort

Use a Merge Tool

For complex conflicts, use a visual merge tool:

git mergetool

This opens a graphical or text-based conflict resolution tool, depending on your setup.

Verify Changes

After resolving conflicts and completing the merge, test the merged code to ensure it works as expected.

How to Squash and Merge in Git

Version 1:

image

Squashing commits refers to combining multiple commits into a single, clean commit. This can be useful when you want to keep a project history neat and concise. Here's a guide on how to squash and merge branches using the command line.

Version 2:

1. Check out the Target Branch

First, ensure you're on the branch you want to merge changes into (e.g., main or develop):

git checkout target-branch

2. Fetch Latest Changes

Before performing any merges, it's important to fetch the latest changes from the remote repository:

git fetch origin

3. Checkout the Feature Branch

Switch to the feature branch (the one with the commits you want to squash):

git checkout feature-branch

4. Rebase Interactively

Rebase the feature branch onto the target branch with an interactive rebase:

git rebase -i target-branch

This will open a text editor with a list of commits in the feature branch. For example:

pick f7f3f6d Added new feature
pick 310154e Fixed bug in new feature
pick a5c7f63 Improved performance of new feature

5. Squash the Commits

In the text editor, change pick to squash (or simply s) for all commits you want to combine, except for the first one. For example:

pick f7f3f6d Added new feature
squash 310154e Fixed bug in new feature
squash a5c7f63 Improved performance of new feature

Save and close the editor.

6. Edit the Commit Message

Git will now combine the selected commits into one. It will open a new editor to edit the commit message. You can modify the commit message to summarize the changes, or keep the default one.

Save and close the editor after editing the commit message.

7. Push the Squashed Commit

Now, the commits have been squashed into a single commit on your feature branch. You need to push the changes to the remote repository:

git push origin feature-branch --force

Note: Using --force is necessary because you have rewritten the commit history. Be cautious with --force, as it overwrites the remote history.

8. Merge the Squashed Commit into the Target Branch

Now that the commits are squashed, you can merge the changes into the target branch. First, switch back to the target branch:

git checkout target-branch

Then, merge the squashed feature branch:

git merge feature-branch

9. Push the Changes to the Remote Repository

Finally, push the merged changes to the remote repository:

git push origin target-branch

Alternative: Squash and Merge via GitHub (or Other Platforms)

If you're using GitHub (or similar platforms), you can squash and merge directly from the web interface. Here's how:

  1. Create a pull request for the feature branch.
  2. On the pull request page, click the "Squash and merge" button.
  3. GitHub will squash all the commits into a single commit and allow you to edit the commit message.
  4. Click "Confirm squash and merge" to complete the process.

This approach is often used in team workflows to ensure a clean history without the need to run commands manually.

⚠️ **GitHub.com Fallback** ⚠️