Skip to content

testA113/git-help

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Git help

Remove local changes and get the HEAD from a branch

Stash local changes

git stash --include-untracked

Go back to the remote branch head

git reset --hard FETCH_HEAD

Undoing changes on a remote branch

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

What to do if you’re royally f***ed

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

Removing local commits

Removes the most recent commit

git reset HEAD~

Rebasing

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.

Edit old commit in branch

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

Apply a diff from a pr to another git project

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

Apply a diff from a pr to another directory in the same project (monorepo)

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

Apply a commit from another branch

git cherry-pick <hash>

Apply commit from a commit in another project

git format-patch sha1^..sha1
cd /path/to/2
git am -3 --reject --whitespace=fix /path/to/1/0001-...-....patch

Apply a commit from a branch to another branch in the same project

git diff HEAD^ | git apply --reject -p3 --whitespace=fix --directory=package/server-ce/

script here

Change git branch name

​​git branch -m old-branch-name new-branch-name
git push origin -u new-branch-name
git push origin --delete old-branch-name

Who deleted some text

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

Steal files / folders from another branch

git checkout <branch_name> -- <paths>

Gitignore isn’t respected?

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).

Delete local branch

git branch -d <branch>

Delete remote branch

git push <remote> --delete <branch>

Amend the most recent local commit

git commit -a --amend

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages