Vincec's Dimension

Git

Word count: 797 / Reading time: 4 min
2018/06/19 Share

Common Clone Command / Git 常用 clone 命令

1
2
3
4
5
6
7
8
9
#Cloning to a specific folder
git clone <repo> <directory>

#Cloning a specific tag
git clone -branch <tag> <repo>

#Clone from ssh
git clone ssh://john@example.com/path/to/my-project.git
cd my-project

Git config

1
cat .git/config # to check the config

Git push / fetch / pull

1
2
3
4
5
6
7
8
9
10
11
12
git push
git push -u origin master # tracking to origin/master

#从远程拉取最新版本 到本地, 自动合并 merge
git pull # git pull origin master = git fetch origin master:tmp && git diff tmp && git merge tmp

#从远程获取最新版本 到本地, 不会自动合并 merge
git fetch
#nomoral use:
# git fetch origin master
# git log -p master..origin/master
# git merge origin/master

Git Reset –hard and –soft && Git Revert

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
### git reset
## hard 不保留原始 commit 结点的任何资讯, 直接将工作区、暂缓区及 git 目录都重置成目标Reset结点的资料内容。
git reset --hard HEAD #to newset version
git reset --hard HEAD~ #to last upload version
git reset --hard HEAD~n #to last nth upload verison

# check most recent n logs
git log --oneline -n

# then enter the version number
git reset --hard HEAD #master
git reset --hard HEAD~
git reset --hard HEAD~2
git reset --hard HEAD~3

# success output
HEAD is now at 4f0f054 test2

# 你回复之前的版本后,又想回复到最新提交的版本 【反悔
git reflog #list all the log
git reset --hard commit_id #with commit_id


## 不会异动到目前所有的实体档案内容;也会保留暂缓区资料内容,让暂缓区与 git 目录资料内容是一致的。
git reset --soft HEAD / commit id

git reflog
git reset --soft HEAD~3

### git revert
git revert HEAD #增加一个 Commit 来取消, Revert some existing commits

Set Git Proxy / 设置git代理

1
2
3
4
5
6
7
8
# set git proxy
# shadowsocks的本地端口默认是1080
git config --global http.proxy ‘socks5://127.0.0.1:1080’
git config --global https.proxy ‘socks5://127.0.0.1:1080’

# cancel git proxy
git config --global --unset http.proxy
git config --global --unset https.proxy

More Commands


Init a local repo and push to github

1
2
3
4
#create a new blank repo in github

git remote add origin https://github.com/mbcrump/SampleProject.git
git push -u origin master

Git clone from private repo of github need add ssh rsa

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#1. Generating a new SSH key
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

Enter a file in which to save the key (/home/you/.ssh/id_rsa): [Press enter] #enter is default

Enter passphrase (empty for no passphrase): [Type a passphrase]
Enter same passphrase again: [Type passphrase again]

#2. Adding your SSH key to the ssh-agent
eval "$(ssh-agent -s)" #Agent pid 59566

ssh-add ~/.ssh/id_rsa

#3. Adding a new SSH key
#install xclip or manual copy from /.ssh/
xclip -sel clip < ~/.ssh/id_rsa.pub

# Github -> Settings -> SSH and GPG keys. -> New SSH key or Add SSH key.

#make config file local
git config --global user.name "John Doe"
git config --global user.email johndoe@example.com

#check list
git config --list
#user.name=John Doe
#user.email=johndoe@example.com
#color.status=auto
#color.branch=auto
#color.interactive=auto
#color.diff=auto

Reference

Author: VINCEC

Permalink: https://vince-amazing/blog/2018/06/19/git/

Date: June 19th 2018, 2:11:40

Copyright license: The article usingCC licensing 4.0

CATALOG
  1. 1. Common Clone Command / Git 常用 clone 命令
  2. 2. Git config
  3. 3. Git push / fetch / pull
  4. 4. Git Reset –hard and –soft && Git Revert
  5. 5. Set Git Proxy / 设置git代理
    1. 5.1. More Commands
  6. 6. Init a local repo and push to github
  7. 7. Git clone from private repo of github need add ssh rsa
  8. 8. Reference