Undo Last Git Commit with reset

The easiest way to undo the last Git commit is to execute the “git reset” command with the “–soft” option that will preserve changes done to your files. You have to specify the commit to undo which is “HEAD~1” in this case.

The last commit will be removed from your Git history.

git reset --soft HEAD~1

In order to undo the last commit and discard all changes in the working directory and index, execute the “git reset” command with the “–hard” option and specify the commit before HEAD (“HEAD~1”).

git reset --hard HEAD~1

Move repository from Bitbucket to GitHub with all branches and commits

Step 1 : Create a Blank repository on Github without Readme.md

Step 2 : Moving all code and content from bitbucket

1. Check out the existing repository from Bitbucket:

$ git clone git@bitbucket.org:Cotocuschandan/testrepo.git

2. Now adding new GitHub Repository as upstream remote of the repository which is clone from bitbucket.

$ cd testrepo
$ git remote add upstream git@github.com:devops-school-com/ds-test.git

3. push all branches and tags to GitHub Repository using below commands

$ git push upstream master
$ git push --tags upstream

Step 3 : Add URL of New Github Repository as redirect URL

$ git remote set-url origin git@github.com:devops-school-com/ds-test.git

Step 4: At last Clone all branches and tags to GitHub Repository

$ git push --mirror

Edit old commit message

Filter Branch Command Line Method

Add this piece of code to Git Bash for project but update the commit ID and the commit message:

git filter-branch --msg-filter "test $(echo '$GIT_COMMIT') = $(git rev-parse 05163c9b8ba425369c9b1619d43ed17b16604ec3) && echo 'Adding theme files' || cat" -- --all

Force push the commit to Github.com with this:

git push origin master --force

Filter Branch Bash Method

Issue: Can’t read PATH

Add this to file named git-reword-commit inside C:\bin

#! /bin/bash
path_to_git='/c/Program Files/Git/mingw64/bin/'
PATH=$PATH:$path_to_git
echo $PATH
export PATH
REV=$1
MESSAGE=$2
FILTER="test $(echo '$GIT_COMMIT') = $(git rev-parse $REV) && echo $MESSAGE || cat"
git filter-branch --msg-filter "$FILTER" -- --all

Open cmd and go to directory C:\bin then type

bash git-reword-commit

PATH is supposed to make the git command work but get these errors:

': not a valid identifier: export: `PATH
git-reword-commit: line 7: git: command not found
git-reword-commit: line 8: git: command not found

Rebase Method

Issue: Only changes on one branch… if the change happened on Master you’ll only see on Master and not Develop. It also separates the two branches and they no longer intertwine.

Figure out how many commits to go back. If you get an error message you went back too far:

git rebase -i HEAD~133

If you didn’t go far back enough then undo it

git rebase --abort

When you see your commit, use the up and down keys to get to the line. Press “i” to enable editing of text. Change “pick” to “reword” and press “Esc” then type “:wq” which will save the file.

pick e499d89 Corrected syntax error 
pick 0c39034 Adding code prism.js
pick f7fde4a More css updates

# Rebase 9fdb3bd..f7fde4a onto 9fdb3bd
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out

After I saved and closed, it then opened up the commit I renamed and I saved that with “:wq” as well.

Finish the rebase with:

git rebase --continue

Force push the commit to Github.com with this:

git push origin master --force

Error: git master matches more than one

Sometimes a tag will be on origin which has the same name as the branch. Windows in particular has an issue with this and will give you an error (git master matches more than one). You can delete the origin tag with the following code, then you’ll be able to push code to your master branch again using SourceTree

git push origin :refs/tags/master

If all else fails.. push in console mode using this line

 git push origin refs/heads/master