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

  • create a new branch and track remote 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/.git
    
  • If you have git clone a repo with submodule, you can find submodules' folders, but the folders are empty.
    git clone https://github.com/.git
    git submodule init        # Init your local configuration file
    git submodule update      # Will pull submodules you need
    
    Use the above commands and you can have a complete repo.

Git clone

  • git clone a private repo
# Normal case
git clone https://username:[email protected]/username/repository.git
# or later input password
git clone https://[email protected]/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:[email protected]/username/repository.git
  • git clone a single branch
git clone --single-branch --branch  host:/dir.git

Git pull

  • git pull & git pull origin master
    Say you are working on a local branch dev, whose upstream branch is origin/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 :
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 [email protected]:A-organization/A-project-name.git

Then solve conflicts, submmit commit, git push, done.

你可能感兴趣的:(Git ToolKit)