Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
This is a quick reference for Git, the DVCS. If you're looking for more detailed help than what's here, see the “Additional references” listed at the end of this
document. My email address is there as well, and if you have questions I may be able to help answer them.
I've tried to list the most commonly used/useful commands here. They're broken up into a handful of categories describing general activities you perform in
Git. Most commands have multiple variants, listed separately, composed of different options and arguments. A few commands have variants listed under
multiple categories because they have quite varied uses. Options that apply to multiple variants of a command are listed to the far right.
SETTING UP THE REPO
Use these commands to create and configure a repository and get up and running with Git.
git init Create a repo in the current directory.
--bare
git clone <url> Clone the repo at the given URL into a local directory.
Create a bare repo (no working tree)
git clone <url> <dir> Clone into the specified directory.
git config <key> <value> Set the "key" to "value" in the config file. --global
Change user-wide configuration.
git config -–unset <key> Remove "key" from the config file.
--system
git config -e|--edit Edit the config file. Change system-wide configuration.
git add --all Stage everything, including deletions and untracked files.
git rm <file> Stage a file deletion.
git commit Commit staged changes to your local repository. Opens your -m "message"
configured editor for a commit message. Specify commit message on command line.
-C <commit>
git commit <path> [<path>...] Commit changes in the given files and/or directories.
Use the message from the given commit for
git commit -a Stage and commit all changes to tracked files. this commit. Very useful in conjunction
with --amend to reuse the last commit
git commit --amend Alter the most recent commit to also contain staged changes. message (-C HEAD).
HOUSEKEEPING
This group of commands is for moving changes between the working tree, index, and stash. At first, the stash may feel like an odd thing to have in a VCS, but
it will soon become second nature. A typical workflow goes like this:
• code, code, code
• boss brings a hot bug that needs fixing NOW
• git stash
• fix bug
• commit
• git stash pop
• code, code, code
Any time you need a quick place to shove aside your in-progress work, the stash is there waiting.
git reset Unstage all changes. I.e. move them from the index back to the working tree.
git reset <path> [<path>...] Unstage changes to the given files and directories.
git clean -f Delete any non-ignored, untracked files in your working tree. -n
Dry run; only show what
git clean -fd Delete directories as well as files. would be deleted.
git checkout <path> [<path>...] Revert working tree changes to the given files and directories. Doesn't affect staged changes.
git stash Push all changes in the working tree and index onto the top of the stash. (Acts like a stack.)
git stash apply Apply most recent stash to the working tree.
git stash pop Apply most recent stash and remove it (if no conflicts occur). stash@{n}
Use stash number n
git stash drop Delete most recent stash.
instead of most recent
git stash show Show a diffstat of the most recent stash. stash.
git branch <name> Create a branch with the given name at your current commit.
git branch <name> <commit> Create a branch with the given name pointing at the given branch or commit. --track
Use with the second form
git checkout <branch name> Make the given branch your current branch. Your working tree will reflect the above to create a branch
state of the commit at the tip of the branch, and new commits will be applied that tracks another
to the branch. branch, typically a
git checkout <commit> remote branch. Then
Make your working tree reflect the given commit. You won't be on a branch, and
remote operations on the
any commits made will be lost if you don't create a branch to contain them.
new branch will
git checkout -b <name> Create a new branch with your currently checked-out commit as its parent, and automatically use the
check out the new branch. tracked remote branch.
git checkout -b <name> <commit> Like the git branch command of similar form, but checks out the new branch.
git branch -d/-D <name> Delete the given branch. If not an ancestor of your current branch, you must force deletion with -D.
git merge <branch name> Merge the given branch into your current branch.
git merge <branch name> -m "message" Perform a merge using the given message for the commit created by the merge.
git cherry-pick <commit> Copy the given commit to your current branch.
git reset <commit> Move to the given commit, making it your HEAD and the tip of your current branch. Changes introduced are
left in working tree.
git reset --soft <commit> Same as above, but leave changes in the index.
git reset --hard <commit> Same as above, but don't leave changes anywhere.
REBASING
While this really belongs to the section on branches, it's a difficult concept, so I gave it its own spot. If you're coming from a centralized VCS, rebasing will take
some time to wrap your head around. To make it as simple as possible, think of the commits in a branch like they're links in a chain. A rebase lets you measure
out a length of the chain, cut it off at a particular link, then reattach the part you cut off to a different link somewhere else, maybe even on a different chain.
On top of that, a rebase actually reattaches a single link of the chain at a time, and you can tell it to let you modify and reorder the links (commits) as it
applies them. This is called an "interactive" rebase. You can even use it to go back in history and make some changes without even moving anything.
If a rebase is interrupted by a conflict or if you're doing an interactive rebase and choose to modify a commit during the process, you'll have to give Git further
instructions after you do your work. When a rebase stops, it will stop after a commit's changes have been applied to the index and/or working tree, but before
the changes are committed. At that point, you can continue it, abort it, or skip the commit that it stopped on.
git rebase <commit> Take commits in the current branch that aren't ancestors of the given commit, and move them onto the
given commit. (<commit> = green; moved commits = purple)
git rebase --onto <commit2> Like above, but move to the "onto" commit instead. This lets you specify a range of commits
<commit1> to be "clipped" and moved over to any arbitrary commit. (<commit1> = orange;
<commit2> = green; moved commits = purple)
git rebase -i <commit> Rewind all commits back to <commit>, and reapply them, optionally editing commits along the way. In fact, any rebase
operation can be made interactive by supplying this flag.
git rebase --abort Abort an interrupted rebase, returning to the state before the rebase started.
git rebase --skip Skip the current commit and continue with the rebase. The skipped commit won't appear in the final result.
git remote add <name> <url> Add a remote repository with the given name and located at the given URL.
git remote rename <old> <new> Change the name of a remote repository. Updates branch names and the remote configuration as well.
git remote rm <name> Remove a remote repository and its remote-tracking branches and configuration entries.
git fetch Fetch unfetched commits from all branches in the remote named "origin" into remote-tracking branches.
git fetch <remote name> Fetch from the given remote instead of "origin".
git pull <remote name> <branch> Fetch a branch from a remote and merge it into your current working branch.
git push <remote name> <branch> Push the given branch to the given remote.
git diff Show changes in the working tree relative to the index.
git diff <commit1> <commit2> Show changes introduced by going from commit1 to
commit2. --staged|--cached
Show changes that are in the index instead of
git diff <commit1>...<commit2> Show changes that would be applied if you were to merge the working tree.
commit2 to commit1.
git diff <path> [<path>...] Only show changes that affect the given path(s).
git show <commit> Show details of and changes introduced by the given commit.
git show <commit>:<path> Show the content of the given path at the given commit.
ADDITIONAL REFERENCES
For when you need a little more help...
git help Brief list of the most common commands.
git help git Extensive list of commands and options to the "git" command itself.
git help tutorial[-2] Two parts of a rapid tutorial introducing basic Git operations.
git help glossary Glossary of common Git terms to use along with other documentation.