git
LEARning goals
- Initialize a local repository
- Perform basic operation on a local repository (add, remove, stage a file. Perform a (partial) commit with a descriptive message)
-
Git installation
-
Repository initialization
-
Commit
-
Add, stage, remove and move files
-
Show diff
OUTLINE
Installing Git
Installing Git
Windows
Linux
OS X
http://code.google.com/p/git-osx-installer
...or via MacPorts
INSTALLing git gui
Windows
Linux
Configuration
Identity
$ git config --global user.name "John Doe" $ git config --global user.email johndoe@example.com
Editor
$ git config --global core.editor nano
Colors
$ git config --global color.ui true
configuration
ConfiGURATION
ALIAS
Add your favorite aliases to the .gitconfig file located in your home folder
[alias] co = checkout ci = commit st = status br = branch hist = log --pretty=format:'%h %ad | %s%d [%an]' --graph --date=short type = cat-file -t dump = cat-file -p
AUTO-Completion
To enable git commands auto-completion in the bash shell download git-completion.bash to
/opt/local/etc/bash_completion.d (MAC)
/etc/bash_completion.d/ (LINUX)
Creating a Repository
$ git init
CREATING A REPOSITORY
Show Status
SHOW STATUS
Tracking files
SPECIFIC Files:
GROUP OF FILES:
tracking files
TRACKING FILES
TRACKING FILES
staging files
STAGING FILES
SPECIFIC FILES:
GROUP OF FILES:
STAGING FILES
STAGING FILES
Creating a Commit
Specific changes:
All changes:
creating a commit
creating a commit
CREATING A COMMIT
After the commit (if all the modification has been commmitted) the repository is again in a clean state
commit manual of style
- Each commit should contain small atomic changes
( A commit can consist of a single modified line)- Examples
- Add a method
- Fix a bug
- Add the initial version of a new file
- Examples
- You should commit very often
- The commit message should be very explanatory, so that the other users can understand why that modification has been made and how it has been implemented
- The messages can be used later to understand the code with git blame!
commit manual of style
- Avoid situation where thousand of modified lines on tons of different files are committed together
- Avoid commit messages like
- "Update bunch of files"
- "I am adding this..", "I am doing this.." (use imperative form)
Show log
Entire (paged)
Date filtering
SHOW LOG
SHOW LOG
SHOW LOG
SHOW LOG
Show Commits
Last commit
Specific commit
SHOW Diffs
Unstaged changes
Relative to specific revision
SHOW DIFFS
SHOW DIFFS
Removing files
From staging area
$ git rm --cached file.py
From index and file system
REMOVING FILES
REMOVING FILES
REMOVING FILES
REMOVING FILES
REMOVING FILES
- Removing a file manually is not enough to remove it from git
- We can still recover the file!
Moving files
Git tracks content, not files. Although there is a move command...
$ git mv file1 file2
...this is the same as...
$ mv file1 file2
$ git rm file1
$ git add file2
Partial commit
Partial commit
UNDOING THINGS
CHANGE LAST COMMIT
$ git commit --amend
UNSTAGE STAGED FILE
$ git reset HEAD file.py
UNMODIFY MODIFIED FILE (Discard changes)
$ git checkout -- file.py
UNDOING THINGS
CHANGE LAST COMMIT
UNDOING THINGS
Git doesn't delete commits
UNDOING THINGS
UNSTAGE STAGED FILE
UNDOING THINGS
UNMODIFY MODIFIED FILE
LEARning goals
- Initialize a local repository
- Perform basic operation on a local repository (add, remove, stage a file. Perform a (partial) commit with a descriptive message)
CreditS
Git - Lesson 2
By Simone Gaiarin
Git - Lesson 2
init and commit
- 838