Skip to content

git notes

Show the commit message and short hash

git log --pretty=oneline --abbrev-commit

alternatively:

git log  --pretty=format:'%h --> %s'

Undo last commit

Source

$ git commit -m "Something terribly misguided" # (0: Your Accident)
$ git reset HEAD~                              # (1)
<< edit files as necessary >>                  # (2)
$ git add .                                    # (3)
$ git commit -c ORIG_HEAD                      # (4)

Alternative:

git revert --no-commit 0d1d7fc3..HEAD
git commit
git push

Merge branches

git checkout master
git merge dev
git push origin master

Push to different remote branch

git push origin master:newBranch

Fetch a file/folder from diffrent branch

git checkout <branch_name> -- <paths>

To get package.json from dev branch to current branch:

git checkout dev -- package.json

Commit current changes to different (existing )branch

git stash
git checkout other-branch
git stash pop

The first stash hides away your changes (basically making a temporary commit), and the subsequent stash pop re-applies them.

Commit current changes to new branch

git stash
git checkout -b new-branch
git stash pop

Remove folder from entire git history

git filter-branch -f --tree-filter "rm -rf FOLDERNAME" --prune-empty HEAD
git for-each-ref --format="%(refname)" refs/original/ | xargs -n 1 git update-ref -d
echo FOLDERNAME/ >> .gitignore
git add .gitignore
git commit -m "Removing FOLDERNAME from git history"
git gc
git push origin master --force

Sign commits by default

git config --global user.signingkey <YOUR_SIGNING_KEY>
git config --global commit.gpgsign true
git config --global gpg.program gpg