Git ToolKit
Git config
git config -l // list all the config of the repo
git config --global -l // list all global config
git config --global core.whitespace cr-at-eol // (Windows Git): Hide ^M (Carriage Return) in Diff
git config --global core.autocrlf true // 提交时转换为LF(Linux \n),检出时转换为CRLF(Windows \r\n)
Create a new branch
$ git checkout -b serverfix origin/serverfix
Branch serverfix set up to track remote branch serverfix from origin.
Switched to a new branch 'serverfix'
- Create a new feature branch in remote.
# 1. Create a new branch:
git checkout -b feature_branch_name
# 2. Edit, add and commit your files.
# 3. Push your branch to the remote repository:
git push -u origin feature_branch_name
Undo the most recent commit
git reset HEAD~
Git submodule
- If you want to git clone a repo alone with submodules. Use the following command:
git clone --recursive https://github.com/<repo-name>.git
- If you have git clone a repo with submodule, you can find submodules' folders, but the folders are empty.
Use the above commands and you can have a complete repo.git clone https://github.com/<repo-name>.git git submodule init # Init your local configuration file git submodule update # Will pull submodules you need
Git clone
- git clone a private repo
# Normal case
git clone https://username:password@github.com/username/repository.git
# or later input password
git clone https://username@github.com/username/repository.git
Password:
# [!!!] When using two-factor authentication(https://stackoverflow.com/a/52011442/8522166)
# 2fa(https://help.github.com/articles/creating-a-personal-access-token-for-the-command-line/)
$ git clone https://github.com/username/repo.git
Username: your_username
Password: your_token
# or inline
git clone https://username:token@github.com/username/repository.git
- git clone a single branch
git clone --single-branch --branch <branchname> host:/dir.git
Git pull
-
git pull
&git pull origin master
Say you are working on a local branchdev
, whose upstream branch isorigin/dev
.
$ git branch -vv
* dev 328c854 [origin/dev] Chapter_5 outline
master 7dedf87 [origin/master] May 9 discussion (#14)
git pull
= git fetch
+ git merge
git pull --rebase
= git fetch
+ git rebase
git pull <repo> <branch>
:
1st argument by default is the current branch's remote
.
2nd argument by default is the current branch's merge
.
$ git config --get branch.dev.remote
origin
$ git config --get branch.dev.merge
refs/heads/dev
I.e.
git pull
will pull from origin/dev
and merge to current dev
branch.
git pull
can pull origin/master
and merge to current dev
branch.[!!]
Scenario: You are developing on local dev
branch, push to the origin/dev
at times for a pr. Now the master has some new updates and you would like to sync with master. You can use git pull origin master
to pull master's new update and merge to your branch.
Merge branch 'master' of github.com:arthma/iot-book into dev
Git rebase
Merge Two GitHub Repo
Say you have two github repo A and B. A is a copy of B and develop some feature. Now you want to merge A back to B so the new feature can be added to B.
Go to B's repo, git pull A's code.
# In B repo. Pull A code
git pull git@github.com:A-organization/A-project-name.git
Then solve conflicts, submmit commit, git push, done.