Undo/change/fix GIT repositories

Convert

Convert to bare

mv .git/ ../repo.git/
cd ../repo.git/
git config --bool core.bare true

Convert to normal

git config --local --bool core.bare false

Commit history

Show last commits

#git show
#git log -10 --oneline
#git log -10 --pretty=oneline
git log -10 --pretty=format:"%h - %an, %ar : %s"
#git log -10 --pretty=%B

git config --global alias.lg "log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --date=relative"
git lg -10

With diffs

git log -p

Query the local repository

    git reflog
    git reflog -1 | sed 's/^.*: //'

Easy undo

Re-fetch a single file

git fetch
git checkout origin/master -- folder/file

Re-fetch everything

git fetch
git reset --hard origin/master

Undo git add

git reset

Undo git rm

git checkout HEAD folder/file

Fix commit messages

if you forgot to identify yourself,

git commit --amend --reset-author

otherwise if you need to fix e.g. a typo,

git commit --amend

and in case you already pushed it before,

git push --force

Undo commit

Undo last commit or also undo the changes in the code,

git reset HEAD^
#git reset --hard HEAD^

Undo a previous commit – go back to the parent of the offending commit,

git rebase -i 03e6c6f^
REMOVE THE OFFENDING COMMITS FROM THE LIST

Undo push

In case you pushed it already, eventually undo the corresponding push – but this is not practical at all – do not do that – unless you are alone on the project, or you team is really small,

git push -f

because the other users or possibly your own other systems may re-sync by doing,

git pull --rebase

Get rid of everything

git log --reverse --oneline --pretty=format:"%h - %an, %ar : %s" | head -10
git rebase -i 953b5a2^
git push -f

Fix local vs remote

git pull origin master

and check

git log -p -20

Troubleshooting

you’ve commited while loosing sync, and when trying to pull you see

fatal: Not possible to fast-forward, aborting.

==>

git pull origin master --rebase
git push

another way around it would be to

git merge

and eventually compare git log output, identify the additional commits there are and show diffs against those

git diff-tree -p COMMIT

https://stackoverflow.com/questions/13106179/fatal-not-possible-to-fast-forward-aborting/43460847 https://stackoverflow.com/questions/17563726/how-to-see-the-changes-in-a-git-commit http://blog.wafrat.com/fixing-fatal-not-possible-to-fast-forward-aborting-when-upgrading-flutter/ https://stackoverflow.com/questions/17563726/how-to-see-the-changes-in-a-git-commit

Resources

Changing a commit message https://help.github.com/articles/changing-a-commit-message/

Git HowTo: revert a commit already pushed to a remote repository http://christoph.ruegg.name/blog/git-howto-revert-a-commit-already-pushed-to-a-remote-reposit.html

On undoing, fixing, or removing commits in git https://sethrobertson.github.io/GitFixUm/fixup.html

2.4 Git Basics - Undoing Things https://git-scm.com/book/en/v2/Git-Basics-Undoing-Things

How do I navigate to the earliest commit in a Github repository? https://stackoverflow.com/questions/28533602/how-do-i-navigate-to-the-earliest-commit-in-a-github-repository

Graphs in GIT https://paquier.xyz/linux-2/graphs-in-git/

convert

to bare https://stackoverflow.com/questions/2199897/how-to-convert-a-normal-git-repository-to-a-bare-one

to normal https://stackoverflow.com/questions/10637378/how-do-i-convert-a-bare-git-repository-into-a-normal-one-in-place/10637882

commit history

https://stackoverflow.com/questions/7293008/how-to-read-last-commit-comment

https://git-scm.com/book/en/v2/Git-Basics-Viewing-the-Commit-History

https://til.hashrocket.com/posts/8ii3emcrda-show-file-diffs-when-viewing-git-log

easy undo

Re-fetch a single file https://stackoverflow.com/questions/28375418/how-to-pull-a-single-file-from-a-server-repository-in-git

Re-fetch everything https://stackoverflow.com/questions/3949804/force-overwrite-of-local-file-with-whats-it-origin-repo

How do I undo ‘git add’ before commit? https://stackoverflow.com/questions/348170/how-do-i-undo-git-add-before-commit

Restoring deleted files in Git https://www.git-tower.com/learn/git/faq/restoring-deleted-files

commit messages

https://help.github.com/en/articles/changing-a-commit-message

https://gist.github.com/nepsilon/156387acf9e1e72d48fa35c4fabef0b4

fast-forward

https://stackoverflow.com/questions/13106179/fatal-not-possible-to-fast-forward-aborting