Skip to content

Instantly share code, notes, and snippets.

@codeasashu
Last active October 18, 2019 17:01
Show Gist options
  • Save codeasashu/e071bb2c97647f9be176fa10459c9f19 to your computer and use it in GitHub Desktop.
Save codeasashu/e071bb2c97647f9be176fa10459c9f19 to your computer and use it in GitHub Desktop.
Git cheatsheet

Rebase

Saving yourself from rebase conflicts

Ref: https://stackoverflow.com/a/44789323

  1. create a new branch git checkout -b new_clean_branch

  2. apply all changes git merge original_messy_branch

  3. forget the commits but have the changes staged for commit

git reset --soft master
git commit -m "Squashed changes from original_messy_branch"

Merge

Merge only a part of file (patch) into another branch

  1. Cherry pick all the changes from a commit to your target branch - git cherry-pick -n <commit id> (-n only stages the commit but do not commit it directly)
  2. Unstage the changes so that you can add them later after filtering desired part. - git reset
  3. Add only part of patch - git add -p. This will allow you to pick only part of changes you're interested in.
  4. commit your staged changes - git commit -m "changes"

View

View in which commit did a particular line of file was changed

You can get the commit history for a file in command git log -p -- filepath.ext. Then grep the line you want to see

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