git sb
Alias for git status -s && git branch -av
(or git s && git b
).
Show summary of working tree and staging area relative to HEAD,
and of current branch and all others.
Distributed version control system.
Reminder: In my status quo I document the commands from the perspective of how I use them, which necessarily (and by design) involves (over)simplifications.
Please consult the official documentation for full details.
My git workflows are still pretty basic, though they are slightly more sophisticated than they used to be.
I still typically develop directly on the master
branch, though I do occasionally use branches these days.
git sb
Alias for git status -s && git branch -av
(or git s && git b
).
Show summary of working tree and staging area relative to HEAD,
and of current branch and all others.
git d [paths] [--stat]
git dc [paths] [--stat]
Aliases for git diff
and git diff --cached
, respectively.
Show the diff between the working tree and the staging area,
or between the staging area and the HEAD, respectively.
If paths are given, show diff only for files matching them.
With --stat
, show changed files instead of changes.
git add paths…
Add given files and directories in working directory to staging area.
(Note that this can also add the deletion of files to the staging area.)
Use git add .
to add all changes to the staging area.
This also adds files which were previously untracked by git.
(As long as they haven't been .gitignore
d.)
These days, I like to add changes to the staging area
"in real time", as soon as I am confident about them.
This makes it easy to review confident changes
and those still in development separately.
git commit
I sometimes use the -a
option for trivial commits, but I usually git add
files beforehand.
I almost always use the -m
option to specify the commit message on the command line, since it's usually a one-liner, in my case.
I always use single-quote escapes to make sure all characters are automatically escaped. For example:
git commit -m 'This is my commit message. It'\''s really cool!!'
Note the use of '\''
to effectively escape the single quote within the single quote escape.
Because git commit -m "This is my commit message. It's really cool!!"
wouldn't work as intended, at least in bash,
since !!
is still interpreted as a special command even within double quotes.
And forget git commit -m "This is my commit message. It's really cool\!\!"
because it would yield a commit message of "This is my commit message. It's really cool\!\!".
Yeah, shell scripting is screwy like that. So now you see why I ALWAYS use single-quote escapes in this case...
I sometimes use the -e
option, for instance to verify that the first commit line is below 80 characters,
or if I want to easily add further lines to the commit, but then I might omit the -em
options altogether.
I sometimes use git commit --amend
if I screwed up the commit message.
git commit --amend -aC HEAD
is extremely useful to quickly add changes to the most recent commit.
git commit --amend -C HEAD --reset-author
is extremely useful before publishing old or rebased commits.
git push [-f] remote [branch]
Push branch (which defaults to HEAD) to remote.
Use the -f
option to force push if necessary.
Use git push remote :branch
to delete branch in remote.
(You can think of it as "push null to branch in remote".)
Also used to push a tag rather than a branch, but rarely.
git log [branch] [paths…]
Show past commits on branch (which defaults to HEAD).
If paths are specified, show only the commits that affected those paths.
git show [commit] [--stat]
Show commit message and diff.
git rm files…
git rm -r directories…
Deletes a file or directory (respectively) from the working tree, and "adds its removal" to the staging area.
git checkout [-b] branch
Switch to the given branch. It's best to have a clean working tree when doing so.
Rarely, with -b, create given branch pointing at HEAD commit and switch to it.
git checkout [tree-ish] [--] paths…
Revert files matched by paths to the version of them from tree-ish, which defaults to the staging area.
Use --
to explicitly indicate the end of options.
git reset paths…
Pretty much the opposite of git add paths
.
git init [--bare]
Initializes a git repository. Creates a .git
subdirectory unless --bare
.
--bare
option is useful on private origins. I use one for local backups.
git clone url
Clones git repo at url into a new directory.
I almost never use this since I almost always work on my own projects, which are already cloned.
git remote add name url
Sets up remote name pointing at url.
Typical remotes:
private
(backup to local server)live
(push to live website)public
(publish on GitHub).git push -u remote branch
Sets up a remote-tracking branch.
git rebase branch
Rewrites history by replaying commits on top of branch, almost always master
.
Especially useful in the case of long-lived branches, which are rare in my case.
git rebase -i commit
git rebase -i --root
git rebase --continue
git rebase --abort
In my case, exclusively used to squash commits together before publication.
Simply use the r
command for the first commit and f
for all others,
then use git commit --amend -C HEAD --reset-author
to update the date.
It's a good idea to make a copy of the branch before the rebase.
git merge branch
Merges the branch into the current branch.
Typically used to merge a (rebased) branch into master
.
git branch -d branch
git branch -D branch
Deletes a branch, use -D
to force.
git stash
git stash pop
git stash list
Use the stash to temporarily save away current changes to work on something else.
git reset --hard commit
Set HEAD to commit and throw away all changes to the index and working tree.
Useful to nuke failed experiments. Dangerous command, proceed with caution!
Here's my .gitconfig
file (see it on github).