Skip to content
Dave Anderson edited this page Aug 13, 2013 · 32 revisions

Setup

Set global identifiers

These will be included plain text in all your commits

$ git config --global user.name "My Name"
$ git config --global user.email [email protected]

On a per repository basis, you can go

$ git config user.name "Myname Forthis Repository"
$ git config user.email [email protected]

Set up a repository

git init directoryname

or

git clone https://host.com/path/to/repository

or

git clone [email protected]/pathtorepo.git

Init

git init creates a hidden .git directory that will be used for the repository.

Clone

git clone copies a remote repository. You now have a local copy with some remote pointers to the repository you copied from.

Add New and Modified Files to Staging

git add .

or

git add filename1.txt filename2.txt etc.txt

Git has a staging phase and a commit phase. You add files to track them and get them ready for a commit.

There is an added benefit: Adding/tracking works like a temporary commit, you can add a file to be tracked, do some more development on the file, then do a diff on the file to see the changes you've made since the add.

When you do an add, all the objects added are added to the git repository object directory.

Review Changes

Status

git status

Status display two sections: staged files ready for commit and changed but unstaged files.

Typical output:

# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#	modified:   hello.txt
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#	handshake.txt

Diff

git diff

Diff will show all modified lines that have not been staged yet.

Typical output:

diff --git a/hello.txt b/hello.txt
index 18832d3..ed3193f 100644
--- a/hello.txt
+++ b/hello.txt
@@ -1 +1 @@
-Hello.
+Hi.

To show the diff of staged files

git diff --cached

Commit

git commit -m "Commit message"

or

git commit # brings up a text editor for you to add a commit message

This commits your changes in staging to the repository.

When you commit, a new commit-tree object is added and the current working branch is updated to point to it. No other objects need to be created since there were already created with the add command.

HEAD -> master -> latest-commit-tree -> latest-top-level-tree.

latest-commit-tree -> previous-commit-tree, so commit history is preserved.

Typical output:

[master aa5448d] Commit message
 1 file changed, 1 insertion(+), 1 deletion(-)

aa5448d is the first seven characters of the hash of the commit text file In this commit, one file changed, one line was added, one line was deleted.

Commit -am Shortcut

git commit -am "Commit message"

This stages any modified files and then does a commit in one command.

Note: New files are not included in this command.

History

git log

This print the recent commit history of the repository.

Typical output:

commit aa5448d2d0a0b4a1e7de9b61937c48a8ebc26a8d
Author: Author Name <[email protected]>
Date:   Sat Jul 27 13:52:32 2013 -0400

    Commit message

Stash

git stash

Creates a temporary holding place of your work.

git stash apply

Moves back stashed work onto your branch.

Undoing

git commit --amend -m "My new message"

This will update your last commit with a new commit message and any staged files.

git reset HEAD

This unstages all staged files

git reset HEAD filename1 filename2

This unstages the specified files; it could be a list of names, a directory, a wildcarded name

git reset --hard HEAD

This unstages all staged files, deleting all newly staged files

git checkout

Repreforms the checkout from the branch but includes the staged changes. Unstaged changes are reverted.

git checkout -- filename *.txt

This reverts unstaged filename, space separated list of filenames, wildcarded names, directories to what it is in the working branch.

git clean -f

This deletes away all unstaged changes.

Clone this wiki locally