Stash local changes
git stash --include-untracked
git reset --hard FETCH_HEAD
Add a new commit, that undoes the changes added by the commit at HEAD. Can then be pushed to the remote repo without messing with other peoples commit histories.
git revert HEAD
Use git reflog to look back in time
git reflog
Find a commit hash just before you stuffed something up, and checkout
git checkout [commit hash]
Bring the head to this commit
git switch -
Make sure this location has the correct state and history that you want. … if so
git reset --hard [commit hash]
If you are happy with your location and changes, then do a force push so the remote is up to date
git push -f
Removes the most recent commit
git reset HEAD~
Finish changes in branch Make pr See conflicts Rebase and solve conflicts in pr branch
git push --force # in pr branch
If someone else is on the same branch, ask what to do - git force push will affect them.
https://www.bryanbraun.com/2019/02/23/editing-a-commit-in-an-interactive-rebase/
git rebase -i HEAD~1 # where 1 is the number of commits back you go, it can also be a sha
Change a commit to edit
git reset --soft HEAD~ # to remove commit, but keep changes to edit
Commit the fixed changes, then run
git rebase --continue
If the previous changes are on the remote, then do
git push -f
To rewrite the edited commit on the remote. Otherwise,
git push # is fine
Make the diff file Go to github pr url and add ‘.diff’ Curl the url
Apply the patch with reject
git apply --reject --whitespace=fix ../tmp.diff
Make the diff file Go to github pr url and add ‘.diff’ Curl the url
Apply the patch with reject and directory
git apply --reject -p3 --whitespace=fix --directory=package/server-ce/ ../tmp.diff
git cherry-pick <hash>
git format-patch sha1^..sha1
cd /path/to/2
git am -3 --reject --whitespace=fix /path/to/1/0001-...-....patch
git diff HEAD^ | git apply --reject -p3 --whitespace=fix --directory=package/server-ce/
git branch -m old-branch-name new-branch-name
git push origin -u new-branch-name
git push origin --delete old-branch-name
for commit in $(git log --pretty='%H'); do
git diff -U0 --ignore-space-change "$commit^" "$commit" | grep '^-.*text that was deleted' > /dev/null && echo "$commit"
done
git checkout <branch_name> -- <paths>
Remember to commit everything you've changed before you do this!
git rm -rf --cached .
git add .
This removes all files from the repository and adds them back (this time respecting the rules in your .gitignore).
git branch -d <branch>
git push <remote> --delete <branch>
git commit -a --amend