Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to keep the local file or the remote file during merge using Git and the command line?

Tags:

git

merge

local

People also ask

What is local and remote in git merge?

LOCAL : the "ours" side of the conflict - ie, your branch ( HEAD ) that will contain the results of the merge. foo. REMOTE : the "theirs" side of the conflict - the branch you are merging into HEAD.

Which command will bring the remote file into local by merging the files in git?

Git Remote Add Origin This command will configure the new GitHub repository as the remote repository and make it the source moving forward. As other collaborators make changes to the repository hosted in GitHub, you can then pull those updates down to your local system.

Which command in git can be used to see files in merge?

The status command is in frequent use when a working with Git and during a merge it will help identify conflicted files. Passing the --merge argument to the git log command will produce a log with a list of commits that conflict between the merging branches.

How do I merge local and remote branch changes?

Merge a Remote Branch to a Local Branch in Git by Tracking and Pulling Changes on the Remote Repository. We will now clone a remote repository containing two branches, master and gh-pages . Then, we will create a local branch another-branch and set it to track any and pull changes made on the remote main branch.


You can as well do:

git checkout --theirs /path/to/file

to keep the remote file, and:

git checkout --ours /path/to/file

to keep local file.

Then git add them and everything is done.

Edition: Keep in mind that this is for a merge scenario. During a rebase --theirs refers to the branch where you've been working.


This approach seems more straightforward, avoiding the need to individually select each file:

# keep remote files
git merge --strategy-option theirs
# keep local files
git merge --strategy-option ours

or

# keep remote files
git pull -Xtheirs
# keep local files
git pull -Xours

Copied directly from: Resolve Git merge conflicts in favor of their changes during a pull


git checkout {branch-name} -- {file-name}

This will use the file from the branch of choice.

I like this because posh-git autocomplete works great with this. It also removes any ambiguity as to which branch is remote and which is local. And --theirs didn't work for me anyways.


For the line-end thingie, refer to man git-merge:

--ignore-space-change 
--ignore-all-space 
--ignore-space-at-eol

Be sure to add autocrlf = false and/or safecrlf = false to the windows clone (.git/config)

Using git mergetool

If you configure a mergetool like this:

git config mergetool.cp.cmd '/bin/cp -v "$REMOTE" "$MERGED"'
git config mergetool.cp.trustExitCode true

Then a simple

git mergetool --tool=cp
git mergetool --tool=cp -- paths/to/files.txt
git mergetool --tool=cp -y -- paths/to/files.txt # without prompting

Will do the job

Using simple git commands

In other cases, I assume

git checkout HEAD -- path/to/myfile.txt

should do the trick

Edit to do the reverse (because you screwed up):

git checkout remote/branch_to_merge -- path/to/myfile.txt