Skip to content

Instantly share code, notes, and snippets.

@dragonworx
Last active October 29, 2021 04:52
Show Gist options
  • Save dragonworx/001d3c31aaf0644118567074ec3cfd9a to your computer and use it in GitHub Desktop.
Save dragonworx/001d3c31aaf0644118567074ec3cfd9a to your computer and use it in GitHub Desktop.
Git Snips

Commits

Get hash for commit: git rev-parse HEAD

Stop commit during message edit in VIM: :cq (q! doesn't stop commit)

Adding

Only add tracked files and ignore untracked: git add -u

Merging

During merging if any conflicts, list merge conflict files before attempting to resolve: git diff --name-only --diff-filter=U

Restore file to unresolved merge state (during merge conflicts, in case you messed it up but want to keep going with other conflicts): git checkout -m <FILE>

While solving merge conflicts in git mergetool when you are checked out on branchA and you're merging in branchB:

  • LOCAL = branchA (your local branch)
  • REMOTE = branchB (the branch you're merging in)

Rebasing

When rebasing its the other way around when solving conflicts in your git mergetool:

  • REMOTE = branchA (your local branch)
  • LOCAL = branchB (the branch you're rebasing on)

Checkout

Checkout remote branch by creating local branch with same name: git checkout --track origin/newsletter

Delete remote branch (Careful!): git push origin --delete test

switch to last active branch: git checkout -

Find commit with message

This will find the commit with "RELEASING:" in the message with media-card version at 69.4.2

git rev-list --grep "RELEASING:" --grep "@atlaskit/media-card@69.4.2" --all --all-match

or

git describe --abbrev=0 --first-parent --match 'next-release-start-*' ffb3535

Cleaning

Check which files will be cleaned (dry run): git clean -n

Then clean those: git clean -f

Remove untracked files: git clean -fd

Taking parts of one branch and applying to another

Let's imagine we have a branch called feature/A which was based off develop and somehow we've messed it up and now we want to start again, but not loose any important changes. Let's create a fresh called branch feature/A-take-2 based off develop and only apply a select amount of changes from feature/A we need.

Follow these steps:

  • Ensure you fetch the latest refs to be up to date git fetch origin develop and git fetch origin feature/A
  • Checkout your feature branch git checkout --track origin/feature/A (if not existing locally) or git checkout feature/A (if already exists)
  • Now we are checked out out in our feature branch, let's reset back to the base branch state working directory state but keep the git index knowing about our feature branch git reset --mixed origin/develop
  • git status will show a bunch of modified changes, we're going to rebuild the index by hand
  • Add only the files you want, either individually git add path/file.ext or with a pattern git add src/path*.
  • If you don't want to add untracked files use the git add -u ...
  • Now you've selected the files you want and created an index (check with git status and look for green files) you can switch to the base branch and rebuild a new branch
  • Checkout the base branch git checkout --track origin/develop (if not existing locally) or git checkout develop (if already exists)
  • Now let's create a new branch based off that develop base git checkout -b feaure/A-new
  • Time to commit those select changes git commit -m "initial commit" or git commit if you want a bigger message
  • Now we have a commit on a fresh branch one commit away from develop, nice. But we still have some junk left over if we git status - you'll still see modified and potentially untracked file
  • Blow that stuff away with git reset --hard and check git status - it should be gone
  • If you want to revert any files from feature/A back to their develop state, just git checkout <REF> src/path/file.ext
  • <REF> can be:
    • HEAD~1 - go back one commit (can be more than 1, the most common way to back track in a linear way)
    • HEAD^1 - go back one common ancestor (can be more than 1, more for merge commit history)
    • fae67bb - a specific commit
    • origin/develop - a specific ref to a branch (which points to a commit)

Now you can keep going on that branch and get it back in shape!

Find Branch Base

git merge-base <MAIN_BRANCH_NAME> <FEATURE_BRANCH_NAME>

Use Git Diff to Rebuild Changes from one branch to clean branch

Generate patch file from add commits in branch. git diff <EARLIEST_BRANCH_COMMIT>..<LATEST_BRANCH_COMMIT> > diff.patch

Create clean branch from master/develop.

Apply patch. --3way and other options help apply changes. Some may fail and need resolving. git apply --ignore-space-change --ignore-whitespace --3way diff.patch

Merge conflicts need to be addressed manually and will not trigger automatic solving.

Using Patches to save changes and apply beyond commits

  • add everything to index:
    • git add .
  • create patch from diff (with --cached for only index, --binary to add non-text files eg. mp3):
    • git diff --cache --binary > ~/themchanges.patch
  • checkout state to apply, then apply them:
    • git apply ~/themchanges.patch

Create Patch from branch

Problem: You have a branch with merge conflicts and too many commits and squashing is difficult, you want to "rebuild" the branch from its base with all its changes into a single commit.

Solution: Create a patch from the current branch A back to it's base, then apply that diff to a new branch from the base

  • checkout branch A
  • git diff origin/<A_BASE_BRANCH_NAME>...HEAD > diff.patch
  • checkout branch A_BASE
  • create new branch to apply diff onto
  • Apply and create reject file where conflicts (needs manual fixes) - git apply --reject --whitespace=fix diff.patch
  • Apply using git am (better, with mergetool for conflicts) - git am -3 --ignore-whitespace --ignore-space-change < diff.patch

Aliases

Add these to your ~/.gitconfig file to get some useful aliases:

[alias]
      list = diff-tree --no-commit-id --name-only -r HEAD
      tree = !sh -c 'git lg | head -15'
      branches = for-each-ref --count=30 --sort=-committerdate refs/heads/ --format='%(refname:short)'
      whereami = rev-parse --short HEAD
      conflicts = !sh -c 'git diff --name-only --diff-filter=U | cat'
  • list - show the files in current commit
  • tree - show git visual history limit to 15 lines with color
  • branches - list the recent branches worked on ordered by modification date (descending)
  • whereami - show the current commit id
  • conflicts - during a rebase or merge conflict, list the files which need resolution
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment