git config --global core.editor "'c:/program files/sublime text 3/subl.exe' -w"
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"
Stage All (new, modified, deleted), anywhere in the current repo (even if you're in a child folder when you run the command)
- 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).
- 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).
- Stage new and modified files only (not deletions)
- Stage modified and deleted files only (not new files)
Excellent article on different options
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.
git checkout HEAD -- my-file.txt
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).
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)
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.
- normal commit, message:
"update README"
(this is WIP) - let's assume this creates a commit id ofabc1234
, assume prior commit id is 8888888 - (do other unrelated work, resulting in more unrelated commits being added)
- come back to
README.md
and update it with one further change git add .
(orgit add README.md
)git commit --fixup abc1234
- (do more unrelated work, more unrelated commits)
- now, we make another change to
README.md
and do agit add
, but don't have to specifyabc1234
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. - (repeat steps 6 and 7 as many times as you wish, until you're ready to squash all
README.md
commits into one) git rebase -i --autosquash 8888888
(orgit rebase -i --autosquash abc1234~1
)
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
git log --follow filename
git log -p
git log --name-only
As above, but including added/modified/deleted info:
git log --name-status
git log -1 --format=%B HEAD
--since
and --until
Date format can be e.g. --since=2.weeks
or --since=2 years 1 day 3 minutes ago
--grep
-Gapples
(to find commits adding or removing text containing "apples". Use --patch
for gory details.
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
git log --all-match --grep=frotz --author=Linus
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
What have I changed in my working directory vs what's already staged?
What is staged vs what's in the previous snapshot?
(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
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
https://stackoverflow.com/a/1113140
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.