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
local) branch usinggit checkout local_branch
remote) to the current one (local)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
git merge --strategy-option theirs
git merge --strategy-option ours
~~
Caution: Better alternative is to go through git mergetool and open it with vimdiff and then
:%diffget RE:%diffget LOvimdiff as a mergetool::cquit (or :cq, to exit with an error code) so that next time git mergetool will launch vimdiff again. Otherwise, the file is all messed up with strings like HEADER >>>>>> and is saved as a real file and mergetool does not do anything.If you quit vimdiff midway and would like to reset the merge again,
git reset --merge
git merge remote_branch
git mergetool
or with
git mergetool tool=vimdiff
if git is not configured.
Resize vimdiff window after maximizing using Ctrl+w =
There are 3 windows on top:
Bottom window: final version of the merged file in the local branch, after merging
[c and ]c), changing lines if needed. To add changes from the remote version (i.e. from branch remote_branch), do :diffget RE
Alternatively, BA or LO for base or local.:%diffget RE::%diffget LOIf the lines get misaligned, do :diffupdate
.orig files using git clean -fdgit add filename (check if this is needed with git status)git commit -m 'merged remote_branch with local_branch'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.
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.
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:
-)Lines in branch_2 (and not in branch_1) are in green (and with +)
To use vimdiff, replace git diff with git difftool.
To change a conflicted file (with conflict markers) into the state of its last commit, do git restore filename
git log
or with ` git log -p ` to see all the changes (patches) to files. Other useful options are --pretty=oneline or stat.
.orig files (record of merging) after merging is done usinggit clean -fd
.gitkeep file (conventional name)touch empty_dir/.gitkeep
and tell to .gitignore to not ignore it using
# Inside .gitignore
empty_dir/*
!empty_dir/.gitkeep
git rebase linearizes two diverging branch heads. If we have a diverging branch exp of the main branch, we can dogit 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.