Error on git patch using git am - linux

When I try to perform a patch using:
git am 0001-someFile.patch
but I get this error:
error: patch failed: src/***/file.c:459
error: src/***/file.c: patch does not apply
Patch failed at 0001 someFile.patch
When you have resolved this problem run "git am --resolved".
If you would prefer to skip this patch, instead run "git am --skip".
To restore the original branch and stop patching run "git am --abort".
I am trying to manually merge the conflicts using:
git mergetool --tool=meld
But I am getting:
No files need merging
How can I solve this problem?
I am getting the name of the file which holds the error, but have no idea about the line (it's a big file)
Maybe there is a better way to perform such patching?

I'm in charge of handling all patching at my work. I've had this happen many times. A patch cannot be merged. The reason this is happening is because the master branch has changes that the patch did not take into account, which prevents it from patching correctly. From all my experience, this could be caused by several things:
The person who made the patch failed to pull the master branch and rebase master onto their development branch.
Between the time that the person pulled and the patch was applied enough changes were made to the master branch to stop the patch from applying due to too many conflicts.
The person patched incorrectly.
Here is the flow I've had the most success with. (This is assuming the person is developing on a branch other than master)
Make sure you've added all files and commited all changes.
git checkout master
git pull
git checkout {development branch}
git rebase master (this will bring the development branch up to speed with master)
git checkout -b {submission branch} master
git merge --squash --no-commit {development branch}
git commit -am "Commit Comment Here" (NOTE: This commit comment will be the name of the patch)
git format-patch origin..HEAD
That makes sure your patch is up to date with the origin master branch. Send that patch and hopefully the patch is applied before too many changes are made on the master.

You need to do a 3-way merge:
git am -3 0001-someFile.patch
git mergetool -t meld

Related

Git: Stashed Changes But Still Can't Pull

I have looked all over and followed several different sets of instructions on this but none seems to work for me.
I have a GitHub repository and I have made some changes on my local machine and some on my Linux server and was hoping to merge the two.
In the past, I have used the git stash command and then been able to pull and then pop the stash to merge. I have never done this on Linux but I assumed it would the same as windows. However, even though I have stashed my changes on the server I am still unable to pull and receive this error:
error: Your local changes to the following files would be overwritten by merge:
aFolder/aDifferentFolder/aFile.py
So naturally I checked to see if my stash had gone through using git stash show and I got confirmation that the stash existed because the top line showed this:
aFolder/aDifferentFolder/aFile.py | 2110 +++++++++++++++++++++++++++++++++++++++----------------------------------------
This is very strange. But decided that I didn't even want my changes on the server that much anyway so I decided to delete them using git reset --hard and then tried to pull and was met with the same error!
So in summary a list of things I have tried:
git stash
git reset --hard
git clean -f
git add . && git stash
git reset aFolder/aDifferentFolder/aFile.py
EDIT: Just to add more info here is the message I get upon calling stash:
Saved working directory and index state WIP on master: 1d49f3a Merge branch 'master' of https://github.com/username/repository
HEAD is now at 1d49f3a Merge branch 'master' of https://github.com/username/repository
I never found out why this happened but a solution that worked for me was creating a new branch pushing to that and then merging with the master branch. Afterwards I deleted the useless branch.

Merge the changes from one Git branch (One File) to another locally [duplicate]

I have multiple branches which are branched off the master (each in a separate subdirectory).
Branch1: new development, not yet completely finished
Branch2: hotfix for a problem, but still under test
Branch3: mess around branch, which I will not restore
Before testing of the hotfix is finished I would like to have the code already available in Branch1, so I can continue developing with the fix in place.
(But since my experience with git is not that much I first started to play around with merge in a 3rd branch, especially created to mess around in, before I mess up either Branch1 or Branch2)
In my 3rd branch I first tried the following:
git merge feature/Branch1
but this gave the following error:
fatal: 'feature/Branch1' does not point to a commit
I next did a commit -a in my Branch1 and tried again, but it keeps giving the same error.
What am I doing wrong? What should I do to merge the code from - in this case - Branch1 with Branch3?
First, checkout to your Branch3:
git checkout Branch3
Then merge the Branch1:
git merge Branch1
And if you want the updated commits of Branch1 on Branch2, you are probaly looking for git rebase
git checkout Branch2
git rebase Branch1
This will update your Branch2 with the latest updates of Branch1.
git checkout [branchYouWantToReceiveBranch] //checkout branch to receive branch
git merge [branchYouWantToMergeIntoBranch]
You can use these commands:
git checkout <the branch you want to merge into>
git merge <the branch you want contents from>
To merge one branch into another, such as merging "feature_x" branch into "master"* branch:
git checkout master
git merge feature_x
* Note that the original branch name is now commonly main instead of master. Choose the correct name based on your situation.
This page is the first result for several search engines when looking for "git merge one branch into another". However, the original question is more specific and special case than the title would suggest.
It is also more complex than both the subject and the search expression. As such, this is a minimal but explanatory answer for the benefit of most visitors.
Just in case you arrived here because you copied a branch name from Github, note that a remote branch is not automatically also a local branch, so a merge will not work and give the "not something we can merge" error.
In that case, you have two options:
git checkout [branchYouWantToMergeInto]
git merge origin/[branchYouWantToMerge]
or
# this creates a local branch
git checkout [branchYouWantToMerge]
git checkout [branchYouWantToMergeInto]
git merge [branchYouWantToMerge]
A slightly more long winded approach, but it works none-the-less:
In branch 3:
git fetch origin Branch1
git merge --no-ff origin/Branch1
At this point merge conflicts may occur, save all changes made to files containing merge conflicts
git add -A
git commit -m "Merge"
git push
DONE

How to copy commits from one Git repo to another?

Last week I created a Github repo and forgot to select a license for the repo. Now there are already 3 large commits.
I have asked the 3 contributors if it is ok if I delete the repo and then create it again with the same name and this time selecting the license when creating the repo, and they were fine with that.
Question
Is there a way I can get the commits into the new repo (this time the first commit is the LICENSE file) and still keep the commit meta info?
Is there a way I have get the commits into new repo (this time the first commit is the LICENSE file) and still keep the commit meta info?
Yes, by adding a remote and cherry-picking the commits on top of your first commit.
# add the old repo as a remote repository
git remote add oldrepo https://github.com/path/to/oldrepo
# get the old repo commits
git remote update
# examine the whole tree
git log --all --oneline --graph --decorate
# copy (cherry-pick) the commits from the old repo into your new local one
git cherry-pick sha-of-commit-one
git cherry-pick sha-of-commit-two
git cherry-pick sha-of-commit-three
# check your local repo is correct
git log
# send your new tree (repo state) to github
git push origin master
# remove the now-unneeded reference to oldrepo
git remote remove oldrepo
The rest of this answer is if you still want to add the LICENSE to your previous repo.
Yes. You can place your LICENSE commit as the first commit by rebasing.
Rebasing is gits way of rearranging commit order while keeping all the commit authors and commit dates intact.
When working on a shared repo, it's generally discouraged unless your entire team is git-fluent. For those that aren't, they can just clone a fresh copy of the repository.
Here's how you get your LICENSE commit as the first commit.
1. Update and rebase your local copy
Check out your project and place the LICENSE file in a commit ON TOP of your current 3 commit stack.
#create LICENSE file, edit, add content, save
git add LICENSE
git commit -m 'Initial commit'
Then do an interactive rebase on the master branch to REARRANGE the commits.
git rebase -i --root
It will open an editor. Move the bottom line (your "Initial commit" commit, the most recent commit) to the top of the file. Then save and quit the editor.
As soon as you exit the editor, git will write the commits in the order you just specified.
You now have your local copy of the repository updated. do:
git log
to verify.
2. Force push your new repo state to github
Now that your copy is updated, you have to force push it to github.
git push -f origin master
This will tell github to move the master branch to its new location.
You should only force push in rare occasions like this where everybody working with it is aware of the pending change, else it will confuse your collaborators.
3. Synchronize collaborators to github
Lastly, all the collaborators will have to synchronize to this repository.
First they must have clean repositories as the following command can be destructive if there are unsaved changes.
# make sure there are no unsaved changes
git status
# pull the latest version from github
git fetch
# move their master branch pointer to the one you published to github.
git reset --hard origin/master
That's it. Everybody should be in sync now.
I had a similar problem where I forgot to fork a repo to my github and added several commits before I realized my mistake.
I found a pretty simple solution.
First remove the remote to the original repo
git remote remove origin
Second add a remote to the new fork on my github
git remote add origin <my repo URL>
Then I pushed to origin master and all of my commits showed up on my github.
I used the following approach:
Clone the source repo to a folder like /c/SrcRepo
Clone the destination repo to a folder like /c/DstRepo and switch to the destination branch
In the root folder of the destination repo run the command:
git pull /c/SrcRepo srcBranch --allow-unrelated-histories
No necessary to create an additional remote reference
Based on #Moocowmoo's answer but trying to streamline it bit more
What this does differently is tries to avoid conflicts as much as possible, just assuming that the remote is correct.
It doesn't however handle deleted files well, so there is still a manual element.
# assuming you are already on the branch you want to be
git remote add oldrepo https://github.com/path/to/oldrepo
git fetch oldrepo
# take all or subset of changes from a branch
git cherry-pick --strategy recursive --strategy-option theirs oldestCommitHash^..latestCommitHash
# or take all changes included in a specific merge commit (easiest)
git cherry-pick --strategy recursive --strategy-option theirs mergeCommitHash^..mergeCommitHash
# handling deleted files/unhandled conflicts
# just keep repeating this section
git mergetool
# either c/m or d based on if you want to keep or delete the files
git cherry-pick --continue
Destination Git = UrlD (existing content doesn't matter)
SourceGit = UrlS
git clone UrlS
git remote add origin2 UrlD
git push -f origin2 master
Now the Destination will have the same data as Source(You can also use origin instead of origin2)
you can try this, it's easy and straightforward. This will push all commits before (and including) the hash you use as <last-commit-hash-from-old-repo> to the other repo:
git clone https://github.com/path/to/new-repo.git new-repo
cd new-repo
git remote add old https://github.com/path/to/old-repo.git
git remote update
git merge --allow-unrelated-histories <last-commit-hash-from-old-repo>
git push origin main
if anyone needs to push all commits from a repo to another as a single commit (like I needed), you can simply add --squash to the merge command like this:
git clone https://github.com/path/to/new-repo.git new-repo
cd new-repo
git remote add old https://github.com/path/to/old-repo.git
git remote update
git merge --squash --allow-unrelated-histories <last-commit-hash-from-old-repo>
git push origin main
In my case I needed to find out differences between the old and new repos. So in the new repo added the old one.
git remote add old https://gitlab.site.az/old-blog.git
Fetch all remotes
git fetch --all
Find different commits
git log --graph --oneline --pretty=format:"%h%x09%an%x09%ad%x09%s" --abbrev-commit --date=relative develop..old/develop
Get commits you selected
git cherry-pick SHA1 SHA2 SHA4

After GIT commit, I did GIT push but the message said everything up-to-date

I'm new to Git. We're in Linux, and I just inherited someone's project family.
I recently did:
git add Bom.xml Bom.csv N.cpp makefile ../mm
git commit -a
(said On branch Bom, your branch is ahead of master by 2 commits. use git push to publish your local commits. Untracked files: list of things I don't want to commit anyway). Nothing added to commit but untracked files present.
then
git push
But it said everything up-to-date. I'm not sure where to look to see that my content is pushed up to my branch on the server. I know I had file changes since my last commit. It's a tough thing to search for the answer online. I looked at up-to-date too, and added the git add and the -a to my commit, but it still says up-to-date when I try to push.
Thanks!
Mich
Like the link you mentioned, make sure you have added the file you want to commit into the staging area. Each time you want to commit
run git status to check.
And then run git add file to add file to staging area.
Run git status to check whether the file is added to the stage.
Then run git commit -m "some message" to commit
run git log to check your commit history check whether you have committed successfully
then check your remote branch by running git branch -a
if your remote branch doesn't have your local branch branch-name
then run git push origin branch-name to push your local branch to remote.

How to handle this merge issue in GIT

I am having a situation here. I did some commit to master branch and after move these commit to
other branch x using and
git cherry-pick
and I run git cherry-pick on x branch itself. Now after this git cherry-pick, forgot to run git push command.
Almost at same time my colleague did the git cherry-pick for his commits and did the git push.
When I see the git log on his machine I couldn't find my commit and realize that I forgot to run git push.
I went to my machine, did the git push. But I am seeing this message now
Without a better log output it is hard to tell, what happend.
I guess you both had the same base commit, each of you did a cherry-pick, and you merged both cherry-picks in order to be able to push it.
The log above shows one commit from you (cf5b726), one commit from your colleague (1aa2ecd) and a merge (28529d0) of the first two commits, which seems to originate from a git pull.
And yes, the merge carries the changes of both merged commits. - After all that's exactly the point of a merge. ;)
You can use git log --graph or git log --format=raw (or magit for Emacs) to see the parents of a commit.

Resources