How can I remove last two commits in git and get my commit 1 as changes in working directory? - linux

Here is the scenario:
On a smallish Ubuntu web server I have a git server running. The website is checked out from git repository. I had a bulk of changes that I added and committed. Git diff showed 50+ code files updated and 20000+ image files. I did not paid much attention to this thinking these should be ignored, my fault. A bit stupid but I thought it was quickest to just commit all changes as a bulk. Let's call it commit A
# Commit A
git add .
git commit -m "Changes so far in this year"
I discovered that I forgot to exclude working/output files (huge number of generated images). Other than these files (around 20000) I had about 50+ files with code changes.
After reading online and git manual I understood that best bet was to update .gitignore and generate a list of files to remove and remove cached. This should remove from commit but not the local folder. Let this be commit B
# Commit B
vi .gitignore
git ls-files -ci --exclude-standard -z | xargs -0 git rm --cached
git add .
git commit -m "Cleanup of generated files from commit history"
Trouble is that now my git push fails with following error
git push origin master
Counting objects: 19219, done.
error: pack-objects died of signal 9
error: pack-objects died with strange error
error: child process died of signal 9
error: died of signal 9
error: failed to push some refs to '/srv/gitrepositories/xxxx.git'
Answers on this question about error 9 suggests it might be due to git running out of memory.
My options?
Is commit A & commit B made up of huge
number of objects, which looking at the count above it seems?
Is there a better way to clean this mess up with possible option to remove commit A & commit B altogether from history and get my changes intact?
Idealy I want to go back to stage where my git diff reports only the 50+ code files. Images are now ignored by .gitignore
Can I delete a git commit but keep the changes? Is this what it sounds like? Can I do it twice for both commit A and commit B?

Yes, you can use git reset HEAD~2 to clear the last 2 commit from history permanently while keeping the changes in the working directory, then git push -f to force push your changed history to the remote.
If your repo is shared with others it's not advisable to change your commit history.

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.

git undo effects of pushed commit on repository size

The question of undoing a previous push to a remote repository has been asked several times, but it seems to me that the answers are incomplete. Following the suggestions in Git - Undo pushed commits, I am able to reset a remote repository so that its behavior is as if the last commit never happened. However, it seems that the effects of the undone commit still show up in the size of the git repository.
Here's the hypothetical situation:
A user does git add, git commit that puts a huge file, "BigFatFile.bin" into version control. He pushes the changes to the remote repository. Let's suppose that this file is HUGE, maybe 1 GB. We don't want to maintain such files in git. So we try to undo this bad commit as follows:
Clone the remote repository.
Do git reset --hard HEAD^ to remove the last commit.
Do git push -f to push changes to the remote repository.
This seems to work, except for the fact that the effects of the push are still visible in the size of the remote repository. Its size has grown by 1 GB.
It is possible on the local copy of the repository to do cleanup steps, using git reflog and git gc and git repack, which reduces the size of the local repository to what it was before the commit of "BigFatFile.bin". But doing a git push (or git push -f) doesn't seem to reduce the size of the remote repository.
How can I permanently undo the last pushed commit so that the size of the repository returns to what it was before the push?
The size of the remote repo shouldn't matter to the clients who are cloning it: the cloned repo should be small again.
As I mentioned in "Clean up large files on git server", you would need to perform the git gc; git repack -Ad; git prune on the server side, in order to ensure the size of the repo there shrinks.
Depending on the server, that might not be possible. For instance, if that was a GitHub-hosted repo, you would need to contact the GitHub support in order to ensure that cleanup task is performed.

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