using Git
Install Git, then set your user name and email address with git config --global user.email "foo@bar.baz" && git config --global user.name "foobarbaz"
, where foo@bar.baz is your email and foobarbaz is your user name.
Create a repository in a local directory, ideally including a README.md
and a LICENSE.md
file, and a .gitignore
file if necessary. Navigate to the repository's directory with cd
, use git init
to create an empty Git repository, then use git add .
to update the index. Use git commit -m "Initial commit"
to establish an initial Git commit with the message Initial commit. Single-line commit messages act as summaries, the convention is to write using imperative mood, start with a capital letter, and end without a period1.
To ignore an already-committed directory or file, add it to .gitignore
, commit the .gitignore
file, then use git rm --cached foobar
, where foobar is the directory or file to be ignored. The directory or file will not be deleted, and it will now be ignored by Git.
action | script | representation | notes |
---|---|---|---|
amend the most recent commit message before pushing it to a remote repository | git commit --amend -m "foobar" |
foobar represents the new message | |
create a commit without a commit message | git commit -a --allow-empty-message -m '' |
||
create a maximally-compressed zip archive of the local repository | git archive --format=zip -9 -o /foo/bar/baz.zip HEAD |
/foo/bar/baz.zip represents the path to the zip archive file. | |
detect changes | git ls-files -m |
||
download a copy of all the versioned data in a Git repository | git clone git://foo.bar/baz.git /foo/bar 2 |
git://foo.bar/baz.git represents the URI of the Git repository, and /foo/bar represents the directory the data will be downloaded to. | If /foo/bar is omitted, the data will be downloaded to a newly-created baz directory. |
get the commit count across all branches | git rev-list --all --count |
||
list all commits in the commit history of branches and tags | git log --all |
||
list all commits with deleted files | git log --diff-filter=D --summary |
||
rename the shortname of a remote | git remote rename foo bar |
foo represents the original shortname and bar represents the new shortname. | |
set the default text editor | git config --global core.editor "foobar" |
foobar represents the command that will launch the text editor from the command line. | |
verify the data integrity of a Git repository | git fsck |
fsck is an abbreviation of file system consistency check.3 The --full option is enabled by default. |
|
view a specified commit | git show foobar |
foobar represents the 40-character commit object name. |
GitHub-specific information
action | script | representation | notes |
---|---|---|---|
change the remote repository on GitHub | git remote set-url baz https://github.com/foo/bar.git |
foo represents the username, bar represents the repository name, and baz represents the shortname of the remote. | |
connect a local repository with a remote repository on GitHub | git remote add baz https://github.com/foo/bar.git |
foo represents the username, bar represents the repository name, and baz represents the shortname of the remote. | |
pull any changes from the connected remote repository on GitHub and add those changes to the local repository | git pull baz master |
baz represents the shortname of the remote. | |
push the local repository to the connected remote repository on GitHub | git push -u baz master |
baz represents the shortname of the remote. | Enter the user's GitHub username and password at the resulting prompt. It may be useful to first simulate the Git command by including the “dry run” -n option, as with git push -n -u baz master . |
licensing
No rights reserved: CC0 1.0, excepting the section below.
prior work
- The
-n
or--dry-run
Git option was introduced to me by an answer on Stack Overflow by YSC. - The convention of writing single-line Git commit messages using imperative mood, to start them with a capital letter, and to end them without a period was introduced to me by a page on the GitHub Erlang/OTP wiki.
- The functionality of
git log --all
was introduced to me by an answer on Stack Overflow by user743382. - The importance of starting a repository with a
README.md
,LICENSE.md
, and.gitignore
file was introduced to me by the default repository instructions on GitHub. - The method of amending the most recent commit message before pushing it to a remote repository was introduced to me by an answer on Stack Overflow.
- The method of connecting a local repository with a remote repository on GitHub was introduced to me by the default repository instructions on GitHub, and further explained in “A step-by-step guide to Git” by Kedar Vijay Kulkarn on Opensource.com.
- The method of creating a commit without a commit message was introduced to me by an answer on Stack Overflow by Jeremy W. Sherman.
- The method of creating a maximally-compressed zip archive of the local repository was introduced to me by a comment on GitHub by scofield-ua and a page on git-scm.com.
- The method of detecting changes was introduced to me by an answer on Stack Overflow by Christoffer Hammarström.
- The method of downloading a copy of all the versioned data in a Git repository was introduced to me by a page on git-scm.com.
- The method of getting the commit count across all branches was introduced to me by an answer on Stack Overflow.
- The method of ignoring an already-committed directory or file was introduced to me by an answer on Stack Overflow by manojlds.
- The method of listing all commits in the commit history of branches and tags was introduced to me by an answer on Stack Overflow by Biffen.
- The method of listing all commits with deleted files was introduced to me by an answer on Stack Overflow by I82Much and an answer on Stack Overflow by Robert Munteanu.
- The method of renaming a shortname of a remote was introduced to me by a page on git-scm.com.
- The method of setting the default text editor for Git was introduced to me by an answer on Stack Overflow by digitaldreamer.
- The method of setting the email and user name was introduced to me by a page on git-scm.com.
- The method of verifying the data integrity of a Git repository was introduced to me by an answer on Stack Overflow by CodeWizard.
- The method of viewing a specified commit was introduced to me by an answer on Stack Overflow by Graham Borland.
less-freely-licensed section
To discard unstaged changes that are not in the index, use git stash save --keep-index --include-untracked
, then drop the stash with git stash drop
.
action | script | representation | notes |
---|---|---|---|
list all of the already committed files being tracked by one's Git repository | git ls-tree --full-tree -r HEAD |
||
show files managed by Git | git ls-files |
It is limited to the current directory and any subdirectories. | |
undo the initial Git commit by deleting the branch one is on | git update-ref -d HEAD |
licensing
Some rights reserved: CC BY-SA 3.0. Includes significant content from:
- A question on Stack Overflow by Readonly
- An answer on Stack Overflow by CB Bailey
- An answer on Stack Overflow by Greg Hewgill
- An answer on Stack Overflow by karlphillip
- An answer on Stack Overflow by vonbrand
with changes made.
prior work
- The listing of files in Git was introduced to me by an answer on Stack Overflow by karlphillip.
- The method of discarding unstaged changes that are not in the index was introduced to me by a question on Stack Overflow by Readonly and an answer on Stack Overflow by Greg Hewgill.