cheatsheet:git

Git

git diff master Branch1 > ../patchfile
git format-patch -1 <sha>

Do once

$ git remote add upstream <git url>

Do each time you want to update

$ git fetch upstream

# then: (like "git pull" which is fetch + merge)
$ git merge upstream/master master

# or, better, replay your local work on top of the fetched branch
# like a "git pull --rebase"
$ git rebase upstream/master
git reset –hard HEAD~

or

git rebase -i HEAD~2

That command will open your default text editor with your two (Change the number 2 with the number of commits you want to get) latest commits:

pick d9f1cf5 Changed again
pick 46cd867 Changed with mistake

# Rebase 105fd3d..46cd867 onto 105fd3d
#
# 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
#
# If you remove a line here THAT COMMIT WILL BE LOST.
# However, if you remove everything, the rebase will be aborted.
#

We just need to delete the line that corresponds to the commit we want to delete and save the file.

To remove a commit you already pushed to your origin or to another remote repository you have to first delete it locally like in the previous step and then push your changes to the remote.

git push origin +<branch>
git reflog

Keep track of the identifier to that commit (you can use either the 1st or 2nd columns). Let's call the identifier “ID”. If you have not made any extra work since you did the reset –hard you can do:

git reset --hard ID
git push -f origin master

If you have made other work since the reset, you could cherry-pick if back onto your branch like this:

git cherry-pick ID
git push origin master
  • List unreferenced object
git fsck --unreachable
  • List unreferenced commit
git show --no-patch $(git fsck --unreachable | grep commit | cut -d' ' -f3)
  • Create branch with the lost commit
git branch <new-branch> <lost-commit-hash>
  • Cherry pick the commit on the current branch
git cherry-pick <lost-commit-hash>
  • If the commit was a stash
git stash apply <lost-stash-hash>
git stash branch <lost-stash-hash>
git stash store -m "On master: lost stash message" <lost-stash-hash>
git branch -m master main # history unchanged
git push origin HEAD
git branch -u origin/main main
git branch -D master
export GIT_SSL_NO_VERIFY=true
  • cheatsheet/git.txt
  • Last modified: 2024/10/14 20:59
  • by 127.0.0.1