Git ToolKit
Git config
1 | git config -l # list all the config of the repo |
Create a new branch
create a new branch and track remote branch
1
2
3$ 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
2
3
4
5
6
7# 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
1 | git reset HEAD~ |
Git submodule
If you want to git clone a repo alone with submodules. Use the following command:
1
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.
1
2
3git clone https://github.com/<repo-name>.git
git submodule init # Init your local configuration file
git submodule update # Will pull submodules you needUse the above commands and you can have a complete repo.
Git clone
git clone a private repo
1
2
3
4
5
6
7
8
9
10
11
12
13# 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.gitgit clone a single branch
1
git clone --single-branch --branch <branchname> host:/dir.git
Git pull
git pull
& git pull origin master
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’sremote
.
2nd argument by default is the current branch’smerge
.
Say you are working on a local branch dev
, whose upstream branch is origin/dev
.
1 | $ git branch -vv |
git pull
will pull from origin/dev
and merge to current dev
branch.git pull origin master
can pull origin/master
and merge to current dev
branch.[!!]
Example:
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.
1 | $ git pull origin master |
Merge Two GitHub Repo
Say you have two github repo A and B. A is a copy of B and develop some features. 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.
1 | # In B repo. Pull A code |
Then solve conflicts, submmit commit, git push, done.