Terminology
- Working Directory
-
Staging Area / Staging Index / Index
Basic Commands
Create or Clone a Repository
- Check the repository’s status
git status
- Initial a local repository
git init NAME
- Clone a remote repository to local (default branch: master, default remote name: origin)
git clone URL
Review a Repository’s History
- List all commits
git log
--oneline
--graph
--all
-p
or--patch
--stat
- List specific commit
git show SHA
Add Commits to a Repository
- Add
- Add files to staging area
git add FILE1 FILE2
- Add all changed files to staging area
git add .
- Add files to staging area
- Commit
- Record changes in staging area to the repository
git commit
- Use the given message as the commit message
git commit -m MESSAGE
- Record changes in staging area to the repository
- Diff
git diff
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
Working Directory <------+-------+-------+
| | | |
| | | |
V | | |
"git add" | | diff
| | | |
| | | |
V | | |
Index <----+-------|-------|-------+
| | diff HEAD |
| | | |
V | | |
"git commit" diff --cached | diff HEAD^
| | | |
| | | |
V | | |
HEAD <----+-------+ |
| |
| |
V |
previous "git commit" |
| |
| |
V |
HEAD^ <--------------+
Good Commit Messages
- Explain what the commit does in the first line
- Explain why in the body
- No how
Tag, Branch and Merge
- Tag
- Create tag
git tag TAG
- Create tag with annotation
git tag -a TAG
orgit tag --annotate TAG
- Delete tag
git tag -d TAG
- Create tag
- Branch
- Create branch
git branch BRANCH
- Delete branch
git branch -d BRANCH
- Switch branch or restore working tree files
git checkout BRANCH/TAG/SHA
- Create a branch and switch to it
git checkout -b BRANCH
- Create branch
- Merge
git merge BRANCH/TAG/SHA
Undoing Changes
- Relative Commit References
^
indicates the parent commit~
indicates the first parent commit
- Replace the tip of the current branch by creating a new commit
git commit --amend
- Revert an existing commit
git revert SHA
- Reset current HEAD to the specified state
git reset SHA
--mixed
--soft
---hard
- Video
Git Collaboration
Working With Remotes
- Remote
git remote
-v
add REMOTE URL
rename OLD NEW
remove REMOTE
- Update remote refs along with associated objects
git push
- Download objects and refs from another repository
git fetch
- Fetch from and integrate with another repository or a local branch
git pull
=git fetch
+git merge
Working On Another Developer’s Repository
- Fork repository
- Summarize ‘git log’ output
git shortlog
-s
-n
--author=NAME
--grep=REGEX
Staying In Sync With A Remote Repository
- Pull request
- Stay in sync with source project
- origin
- upstream
- Reapply commits on top of another base tip
git rebase -i BASE