Merging and Resolving Conflicts via CLI - bounswe/bounswe2024group11 GitHub Wiki
You can access the general git tutorial here
Follow these steps to merge branches using the command line.
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
).
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.
If conflicts arise during a merge, follow these steps to resolve them.
List all files with conflicts:
git status
Files with conflicts will be marked as both modified
.
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 >>>>>>>
).
After resolving conflicts in a file, mark it as resolved:
git add <file>
Repeat this step for each conflicted file.
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.
After resolving the merge, push the changes to the remote repository:
git push origin target-branch
If you want to cancel the merge and return to the previous state:
git merge --abort
For complex conflicts, use a visual merge tool:
git mergetool
This opens a graphical or text-based conflict resolution tool, depending on your setup.
After resolving conflicts and completing the merge, test the merged code to ensure it works as expected.
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.
First, ensure you're on the branch you want to merge changes into (e.g., main
or develop
):
git checkout target-branch
Before performing any merges, it's important to fetch the latest changes from the remote repository:
git fetch origin
Switch to the feature branch (the one with the commits you want to squash):
git checkout feature-branch
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
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.
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.
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.
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
Finally, push the merged changes to the remote repository:
git push origin target-branch
If you're using GitHub (or similar platforms), you can squash and merge directly from the web interface. Here's how:
- Create a pull request for the feature branch.
- On the pull request page, click the "Squash and merge" button.
- GitHub will squash all the commits into a single commit and allow you to edit the commit message.
- 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.