Trying to reverse apply stash, git stash show and git apply giving errors - git-stash

I'm following this git stashing guide but when I try to un-stash by using the command
$ git stash show -p stash#{0} | git apply -R
or
$ git stash show -p | git apply -R
I keep getting these errors
error: patch failed: app/scripts/app.js:20
error: app/scripts/app.js: patch does not apply
error: patch failed: app/views/main.html:34
error: app/views/main.html: patch does not apply
How do I get past this error?
When I do $git stash list it shows stash#{0}: WIP on my_branch: dc19ed5 My Commit

If you've modified the code after applying a stash you won't be able to reverse the applied stash in the manner suggested by that guide. This is because git can no longer apply the patch specified by the stash as the code doesn't look how it expects any more.
You could fix up the patch output by git stash show -p stash#{0} manually, but unless the changes made since the patch was applied were very minor I wouldn't recommend it.
These steps should allow you to get to the state you want, there may be a better way so I'll update this answer if I think of it:
Stash your code in the state it's currently in
Apply the previous stash and commit
Apply the new stash and commit (there may be some merge conflicts which you'll have to fix manually)
Revert the first commit
This should leave you with a commit that only has your new changes. You may want to do this on a branch.

Related

An Excel file prevents me to do interactive rebase in IntelliJ Idea

I face a merge conflict when I would like to rebase the develop branch against my feature branch. I use IntelliJ Idea to execute individual Git commands. Let's say I have 3 pushed commits and I want to squash and rebase them using --interactive mode.
I select all three commits, I select squash, do some rewording, and hit Rebase.
The process fails and an Excel file that appears in the Git panel among Changes and on the diff panel, says the contents are the same. I can only abort or retry the rebase, but continuing doesn't make sense as the change always prevents me to continue.
Here is the console output:
12:50:42.385: [myapplication] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false -c core.commentChar= rebase --interactive develop
Rebasing (1/2)
error: Your local changes to the following files would be overwritten by merge:
excel-file.xlsx
Please commit your changes or stash them before you merge.
Aborting
hint: Could not execute the todo command
hint:
hint: fixup 37d1fc57dff9c7e9a312c88cf78c176cb71dbb47 CommitMessageIsHere
hint:
hint: It has been rescheduled; To edit the command before continuing, please
hint: edit the todo list first:
hint:
hint: git rebase --edit-todo
hint: git rebase --continue
Could not apply 37d1fc5... CommitMessageIsHere
Is there a trick to overcome this obstacle? Is possible to somehow force ignoring and skipping such merge conflict?
You started your rebase with un untracked copy of excel-file.xlsx, and when git met a commit which included the action "create a new excel-file.xlsx file", he stopped.
You have to somehow put aside this untracked version before proceeding.
The simplest way is:
rename that file on disk:
mv excel-file.xlsx excel-file.xlsx.tmp
run git rebase --continue:
either IntelliJ has a button which looks like "proceed with the rebase", or you can run this command from the command line,
once the rebase is complete:
you can compare the contents of both files, and decide what the correct action is -- update the tracked version, delete the tmp file ...

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.

Git fatal: cannot simplify commit

I have a repo that I have been committing for 2 months. I do not seem to get any error when I commit daily. However I was looking at git logs and I see that first ever git log (or maybe git can't see beyond that log point) has an error message like this
:100755 100755 1948ac6... 2af905e... M document.doc"
error: Could not read 190d54eb3278746a4e35fd4be82689eb4b1d20a8
fatal: cannot simplify commit cb0c2a3bf0a4ad665eb376b818bdcb0652a06eec (because of 190d54eb3278746a4e35fd4be82689eb4b1d20a8)
I tried the recommendation on: https://git.wiki.kernel.org/index.php/GitFaq#How_to_fix_a_broken_repository.3F
But I did not get what I was trying to achieve, whatever was recommended did not help my case.
This solution could help you:
Make a backup of your .git directory in case you corrupt things more in this process. Then, put back the best version of the packfiles you have available.
For each of the corrupt packfiles, run:
mv .git/objects/pack/pack-**yourpack**.pack oldpack
git-unpack-objects -r < oldpack
Run git fsck --full and git checkout again, give us the output.
Looks like now you should be able to check out, but you will have to run git checkout -f yourbranch, as you have changes in your working dir that are not yet committed. These changes will get lost when you run git checkout -f, though.
Note: This solution was sugested by #Chronial

Edit an incorrect commit message in command line Git [duplicate]

This question already has answers here:
How to modify existing, unpushed commit messages?
(27 answers)
Closed 8 years ago.
I use Git in command line with linux and not as a graphic client.
I wrote the wrong thing in a commit message.
How do I change the message?
If it is the most recent commit, you can simply do this:
git commit --amend
This brings up the editor with the last commit message and lets you edit the message. (You can use -m if you want to wipe out the old message and use a new one.)
And then when you push, do this:
git push --force <repository> <branch>
Be careful when using push --force. If anyone else has pushed changes to the same branch, those changes will be destroyed.
Anyone who already pulled will not get an error message, and they will need to update (assuming they aren't making any changes themselves) by doing something like this:
git fetch origin
git reset --hard origin/master # Loses local commits
To change a commit message of the most recent (unpushed) commit, you can simply use
git commit --amend -m 'new message'
To change messages of (unpushed) commits further in the past:
git rebase -i [COMMIT BEFORE THE FIRST YOU WANT TO EDIT]
If it is the last patch you commited from your repo, it will be on the top of your git log.
In that case, just run the below command and push the same once again.
git commit --amend
Than, modify your message and push the same. Since you are not modifing any change in file, it should not give any error.
If some patches have already come on the top of yours. Then you have to check merge dependencies also. In this case,
either git reset --hard your commit
run git commit --amend
Push it back
or
git commit --amend -C commit-id
push it back
But you need to consider merge dependencies also.
**
And more best approach will be:
**
You can use git rebase, for example, if you want to modify back to commit xyz, run
$ git rebase --interactive xyz^
In the default editor, modify 'pick' to 'edit' in the line whose commit you want to modify. Make your changes and then commit them with the same message you had before:
$ git commit -a --amend --no-edit
to modify the commit, and after that
$ git rebase --continue
to return back to the previous head commit.

Error on git patch using git am

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

Resources