Skip to content

Instantly share code, notes, and snippets.

@duncanawerbuck
Last active April 28, 2021 14:22
Show Gist options
  • Save duncanawerbuck/b247a3a11d214bcc0a94ca2a18aa3477 to your computer and use it in GitHub Desktop.
Save duncanawerbuck/b247a3a11d214bcc0a94ca2a18aa3477 to your computer and use it in GitHub Desktop.

Config - default editor

Set Sublime Text 3 as default editor for commit messages on Windows:

git config --global core.editor "'c:/program files/sublime text 3/subl.exe' -w"

Set Visual Studio Code as default editor for commit messages on Windows:

Depending on your setup, you may need to replace the path to Code.exe.
git config --global core.editor "'C:/Users/YourUserName/AppData/Local/Programs/Microsoft VS Code/Code.exe' -w"

Different ways to stage files with git add

git add -A

Stage All (new, modified, deleted), anywhere in the current repo (even if you're in a child folder when you run the command)

git add .

  • Identical to git add -A . (note the dot at the end)
  • Stage All (new, modified, deleted) in the current folder and child folders (ignores changes/additions/deletions anywhere higher up the folder structure than the folder you're in when you run the command).

git add -A .

  • Identical to git add .
  • Stage All (new, modified, deleted) in the current folder and child folders (ignores changes/additions/deletions anywhere higher up the folder structure than the folder you're in when you run the command).

git add --ignore-removal

  • Stage new and modified files only (not deletions)

git add -u

  • Stage modified and deleted files only (not new files)

Reverting

Excellent article on different options

Unstage all files

git reset is equivalent to git reset --mixed which does this:

Resets the index but not the working tree (i.e., the changed files are preserved but not marked for commit) and reports what has not been updated. This is the default action.

Hard reset a single file

git checkout HEAD -- my-file.txt

Fixing last 1 or 2 commits

Note: This assumes you haven't pushed to shared history, OR you have pushed, but the branch is exclusively yours and rewriting history will not affect your colleagues, your spouse or your dog).

Stack Overflow answer

Suppose we have A-B-C as the latest three commits and C is the head. What you want is to squash B and C into a single commit which reuses B's commit message:

git reset HEAD~1 --soft
git commit --amend --no-edit

(That SO post shows various options)

Autosquash

As per this Egghead lesson.

If e.g. you're interrupted and you're going to come back and make further commits (after you potentially make other commits that are unrelated to the work you're doing at the moment you're interrupted).

You want to be able to autosquash your current work, so in this example, we're working on a README.md file and let's say you've made one initial change, and you've just been interrupted.

  1. normal commit, message: "update README" (this is WIP) - let's assume this creates a commit id of abc1234, assume prior commit id is 8888888
  2. (do other unrelated work, resulting in more unrelated commits being added)
  3. come back to README.md and update it with one further change
  4. git add . (or git add README.md)
  5. git commit --fixup abc1234
  6. (do more unrelated work, more unrelated commits)
  7. now, we make another change to README.md and do a git add, but don't have to specify abc1234 when we commit with the --fixup flag. We can use a regex search of the target commit message: git commit --fixup :/README. Of course this assumes your search string is unique! So you might want to be a bit smarter with your original commit message - perhaps include a timestamp or work item id that you can search for instead of text that will match very old commits.
  8. (repeat steps 6 and 7 as many times as you wish, until you're ready to squash all README.md commits into one)
  9. git rebase -i --autosquash 8888888 (or git rebase -i --autosquash abc1234~1)

Viewing History

For pretty history with git log, add this to the [alias] section (add if not there) in .config:

lg = log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --branches

History of a single file

git log --follow filename

Show changes introduced in commits

git log -p

Show files affected after each commit

git log --name-only As above, but including added/modified/deleted info: git log --name-status

Show full commit message body

git log -1 --format=%B HEAD

Limit by date

--since and --until Date format can be e.g. --since=2.weeks or --since=2 years 1 day 3 minutes ago

Limit by keyword(s) in commit messages

--grep

Limit by string added or removed

-Gapples (to find commits adding or removing text containing "apples". Use --patch for gory details.

Limit by string in actual content change

E.g. you know there was a commit that changed the function foo: -Sfoo -S. An example of the latter, where you're interested in changes to

Limit by multiple criteria

git log --all-match --grep=frotz --author=Linus

Compare histories of two branches

Say you’re on “nogood” branch and you want to see the commits on master since you branched that are not in the nogood:

git diff nogood..master --oneline

Diffing

git diff

What have I changed in my working directory vs what's already staged?

git diff --staged (alias: git diff --cached)

What is staged vs what's in the previous snapshot?

Compare one file between two specific commits

(credit: to Mark Longair answering this SO question)

git diff master~20:README.md README.md

You can replace master~20 with the SHA of the commit.

Note that this is actually comparing the old README.md to the version in your working tree, not the version committed in master. If you want that, then you can do the following instead:

git diff master~20:README.md master:README.md

Cycle through all files in a single commit using GitHub UI

https://github.com/<org>/<repo>/commit/<commit-sha>/<path-to-file>

e.g.

https://github.com/grails/grails-core/commit/02942c5b4d832b856fbc04c186f1d02416895a7e/grails-test-suite-uber/build.gradle

Find a deleted file and restore it

https://stackoverflow.com/a/1113140

Branching

Viewing and creating

git branch: Shows all your branches

git branch newbranch: Creates a new branch

git checkout -b newbranch: Creates a new branch and switches to that branch immediately. This is the same as git branch newbranch followed by git checkout newbranch.

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