廖雪峰 - Git教程
Coursera - Server-side Development with NodeJS, Express and MongoDB
0. Git Diagram
工作区(Working Directory) --add--> 暂存区(Staging area) --commit--> Master
HEAD refers to master
1. Global Configuration for Git
- Check to make sure that Git is installed and available on the command line, by typing the following at the command prompt:
- To configure your user name to be used by Git, type the following at the prompt:
git config --global user.name "Your Name"
- To configure your email to be used by Git, type the following at the prompt:
git config --global user.email <your email address>
- Check your default Git global configuration:
- Initializes the current folder as a git repository, just go to the folder and type the following at the prompt:
Now the folder will be mark as a master
- Current status of the foler:
- current branch
- untracked files: files that are not been added to Git repository. Use
add to add.
- Changes not staged for commit: still use
add to update
- changes to be committed: files listed below are ready to be committed
- See change details of a file:
- Add files to staging area:
git add <file(s)/folder(s)>
git add .
- After adding some files, you can commit the current state of the folder into Git repository:
- Show the brief log of all the commits with messages
- Discard changes of the last commit, and checkout the file from an older commit. last commit -> any commit (staged)
git checkout <commit> <file>
The file changes will be automatically set into the staging area, all you need to do is submitting.
- Reset a file from staged to untracked: staged -> untracked
git reset <commit [default=HEAD]> <file>
This command essentially has deeper meaning: You can return to past versions by config the
HEAD: current version;
HEAD^: previous version;
HEAD^^: previous previous version...;
HEAD-100: 100 versions before
- Reset the staging area to untracked. (Just status, not actually discarding your changes on the files)
- Discard changes of an untracked file: untracked -> last commit
git restore <file>
git checkout -- <file>
- Return to a future version? Still use commit id as reference in
git reset. But how to know the commit id of a future version? Use
reflog to see command history.
git checkout -- <file>或者
git restore <file>。
git reset HEAD <file>，就回到了场景1，第二步按场景1操作。
git reset HEAD^，不过前提是没有推送到远程库。
###0. Connect with your Github account using SSH
1. Connect with a remote repo
- Suppose you have a blank repo on Github, and an existing repo on your computer
- Connect local repo with a remote one:
git remote add origin email@example.com:username/repoName.git
origin is the default name of a remote repo.
- Push local repo to the remote one:
git push -u origin master
-u is for: Branch 'master' set up to track remote branch 'master' from 'origin'.
After the first push, you don't need the
- Show info of remote repo:
- Disconnect remote repo by name:
2. Clone an existing remote repo
git clone firstname.lastname@example.org:username/repoName.git
Instead of ssh, https is also available.
Current bransh has a
- Merge branch to current one:
When you merge a branch to current branch, sometimes there are conflicts.
The conflicts will be directly showed in the files as:
Creating a new branch is quick & simple.
Creating a new branch is quick AND simple.
- Just fix it manually, then
- Show merge logs:
git log --graph --pretty=oneline --abbrev-commit
git log --graph
stash to save current work
git stash list
git stash pop
cherry-pick to copy a specific commit
git cherry-pick <commit id>
git push origin branch-name，如果推送失败，先用
git checkout -b branch-name origin/branch-name，本地和远程分支的名称最好一致；
git branch --set-upstream branch-name origin/branch-name；
Tag is a name that we give to a commit.
- Set a new tag to the newest commit:
git tag <name>
git tag -a v0.1 -m "version 0.1 released"
To a past commit?
git tag <name> <commit id>
Show info of a tag: