Debdeep Bhattacharya

View My GitHub Profile

Merging a git branch

13 Nov 2021

If we want to merge the branch remote_branch to the branch local_branch (the terms remote and local are well-defined in that sense), we do the following.

git branch local_branch
git checkout local_branch
git merge remote_branch

Note: you may get ‘branch does not exist’ error, in that case, checkout to the remote branch first and then get back to the local one.

git mergetool
git branch -d remote_branch
git push -d origin remote_branch

Replacing all files without resolving conflicts

Caution: Better alternative is to go through git mergetool and open it with vimdiff and then

Using vimdiff as a mergetool:

If you quit vimdiff midway and would like to reset the merge again,

  1. first cancel the merge
git reset --merge
  1. and perform the merge again with
git merge remote_branch
  1. fix merge conflicts
git mergetool

or with

git mergetool tool=vimdiff

if git is not configured.

Bottom window: final version of the merged file in the local branch, after merging

git branch -d remote_branch

Caution: attempting to delete the remote branch without committing the merged local branch first will throw warning sign. That would be a reminder that the current needs to to committed.

Scenarios

Pull from the internet and discard your local changes:

git reset --hard
git clean -f
git pull

Here, clean -f to remove untracked files. clean -fd deletes untracked directories as well. Before clean -f, we can do clean -d to see which files are to be removed.

Pull from the internet and set aside the local (uncommited) changes:

git stash
git pull

Then to pop back the local uncommitted changes on top of that using

git stash pop

To delete the stash, do git stash drop instead.

Desired Method: Pull from the internet and put the current committed changes as next commit after the remote version (nice explanation for the reason to rebase)

git pull --rebase

(or just git pull -r)

This might need merge-conflict resolution

git mergetool --tool=vimdiff

and cleanup

git clean -df

Then, to indicate that rebasing is done, we do

git rebase --continute

followed by git push to push the final local change. (Note that git commit is not required and has been done automatically)

This creates a commit history of the order last remote commit, merged local commit.

This (--rebase) is better for history and creates 2 commit histories compared to just git pull (with default --merge behavior), which creates 3 commit histories in the order last remove commit, last local commit, merged commit.

To recover from a failed rebase and revert to the state before the attempted pull (i.e. local changes remain in local directory), do

git rebase --abort

Note: git pull --rebase should be used over just git pull where we do not want to advertise that a merging has been done, e.g. when working on the same branch. This is the most common scenario.

git diff branch_1 branch_2 -- filename.txt

The output will have the following:

git log

or with ` git log -p ` to see all the changes (patches) to files. Other useful options are --pretty=oneline or stat.

git clean -fd
touch empty_dir/.gitkeep

and tell to .gitignore to not ignore it using

# Inside .gitignore
empty_dir/*
!empty_dir/.gitkeep
git checkout exp
git rebase main

to make the changes of exp as a next commit to the ones in main. At this point, the last commit of main becomes a previous commit for exp. So, we can just merge exp to main using

git checkout main
git merge exp

Q: How to change files with conflict markers to its state before attempted merge?

git merge --abort

Note, for uncommitted files, this might revert the files back to last committed state.