git博大精深,留一些學習上遇到的困惑,當作以後的小抄。
我覺得寫得很棒的教學: Learn git in 30 days.
學習branch、merge的線上圖形遊戲: Learn Git Branching
Commit message該怎麼寫: Commit convention
git init
Initialize a git repo at the current working directory.add
git add <file>
Add new or modified file into Staged.git add -A
Add all changes(including new, modified, and deleted files).git add --all
The same as above.git add .
The same as above.git add -u
Add all modified and deleted. Use this to avoid staging ignore files.commit
git commit
Transfer all Staged files into git repo
and establish a new version.git commit <file>
Commit a specific <file>
. It could be either Staged or Modified.git commit -a
This add
all unstaged(Modified) files automatically. And then commit
. Untracked files won't be add
nor commit
.git commit -m "msg"
msg
is a note for each commit shown in git log
. A commit note is reuquried. Without -m "msg"
, git opens an editor and ask for typing a message.reset
git reset <commit>
Unstage all Staged files, which means, reseting index to <commit>
. And set HEAD to <commit>
. If <commit>
is empty, then resets index to HEAD.git reset --hard <commit>
Unstage files and copy files from HEAD of git repo to the disk. In other words, reset index and working tree in the same time.branch
git branch <branch name>
Create a new branch, staying at current branch(not changing HEAD).git branch -v
Display all the banches, including HEAD
.checkout
git checkout <branch/commit> <file>
Pull specific file from <branch/commit>
to working tree and index
, without changing HEAD
.git chekcout <branch/commit>
Switch HEAD to a specific <branch/commit>
and pull all the files from <branch/commit>
to working tree.merge
git checkout master
git merge hotfix2
: Merge hotfix2
into master.mv
git mv <file name> <new file name>
Rename the file/dir.git mv <file name> <new path>
After changing the file path without git command, git mv <the file> <path>
fails to execute.remote
git remote
Display names of remotes.git remote -v
Display names of remotes and their destinations.git remote show <remote name>
Display more details about a remote.git remote add <remote name> <URL>
Add a remote.git remote remove <remote name>
Remove a remote..gitignore
.gitignore
takes effect immediately right after it is modified without staging or commiting it. .gitignore
can also be ignored.diff
Instruction | Compare 1 | Compare 2 |
---|---|---|
git diff |
Working directory | Current version |
git diff HEAD |
Working directory | Index |
git diff commit_id |
Working directory | Commit version |
git diff --cached/--staged HEAD |
Index | Current version |
git diff commit_1 commit_2 |
Commit 1 | Commit 2 |
git diff HEAD^ HEAD |
Last version | Current version |
git diff <file> |
The file in directory | Current version |
Note: Working directory = Working tree = Unstaged. |
git commit
copies/sends this kind of file into git repo/HEAD. A file in the index is called a Staged file.*
git rm --cached <file>
where<file>
is already committed and you want to remove it from the HEAD.
Typical Flow Example: (from $1~$2)
Typical Flow Example: (from $3~$5)