Skip to content

Instantly share code, notes, and snippets.

@DenisIzmaylov
Last active May 8, 2023 03:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DenisIzmaylov/f8a752a6c6502d9476d6 to your computer and use it in GitHub Desktop.
Save DenisIzmaylov/f8a752a6c6502d9476d6 to your computer and use it in GitHub Desktop.
Git HOW-TO Tricks

Git HOW-TO Tricks

Content

Edit Last Commit Message

git commit --amend

Edit Several Commits Message

  • Mark commits which you want to edit by git rebase -i HEAD~X
  • Edit each commit message by git commit --amend
  • Finish rebase operation by git rebase --continue
  • Push your changes with forcing to the server by git push origin BRANCH -f

Squash commits

  • Make commits which you want to squash by git rebase -i HEAD~X
  • Finish rebase operation by git rebase --continue
  • Push your changes with forcing to the server by git push origin BRANCH -f

Make Pull Requests in GitHub

  • Fork the target repo
  • Checkout your (forked) copy of target repo git checkout your-repo-url
  • Create new feature branch by git checkout -b your-feature
  • Push your feature branch
  • Create Pull Request with this feature branch

Cherry Pick From Another Repository

  • Connect another remote repo: git remote add project-b git://github.com/username/project-b.git
  • Fetch changes from it: git fetch project-b
  • Have a look at commits: git log project-b/master
  • Select a commit to cherry pick to your repo
  • Do it: git cherry-pick 42a5623ecb0db5da356bb0be01dacfe17b8014e5
    • It could be commited immediately
    • It may cause a merge conflicts - you should resolve it and commit with git commit -a
  • Push you changes git push origin master
  • Sometimes it could failed with error Updates were rejected because a pushed branch tip is behind its remote hint: counterpart.:
    • Create cherry-picked changes as a new branch git branch feature-A
    • Reset to origin point, e.g.: git reset --hard HEAD~1
    • Merge and push changes.

Get Churn Count

Churn Count displays how much times a given files has been modified to detect which modules are causing problems most often and should be refactored.

Next command show you the churn counts for an entire Git repo or specific files/directories in it. To get top 10 modified files run:

git log --all -M -C --name-only --format='format:' "$@" | sort | grep -v '^$' | uniq -c | sort -n | awk 'BEGIN {print "count\tfile"} {print $1 "\t" $2}'  | tail -n10 

Get Commits Log in Pretty Style

git log --graph --abbrev-commit --decorate --format=format:'%C(bold blue)%h%C(reset) - %C(bold green)(%ar)%C(reset) %C(white)%s%C(reset) %C(dim white)- %an%C(reset)%C(bold yellow)%d%C(reset)' --all

Get Commits Graph

Next command will format your git log output to display it as a colored graph with commits and merging.

git log --graph --abbrev-commit --decorate --format=format:'%C(bold blue)%h%C(reset) - %C(bold green)(%ar)%C(reset) %C(white)%s%C(reset) %C(dim white)- %an%C(reset)%C(bold yellow)%d%C(reset)' --all

Tools

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment