前言
自己大部分代码都托管到Github上的Public
和Private
仓库上。
1 安装
下载Git
1.1 创建ssh key
安装Git过程中,默认Enter
,打开Git Bash
,
1
|
ssh-keygen -t rsa -C "misaraty@163.com"
|
默认Enter
,在C:\Users\m\.ssh
下有id_rsa.pub
文件,打开并复制key
。
1.2 在GitHub中关联key,
Settings
SSH and GPG keys
New SSH keys
,输入key
。在Git Bash
中,
输入yes
,显示You've successfully authenticated, but GitHub does not provide shell access.
。
1.3 Git关联账号
1
2
|
git config --global user.name "misaraty"
git config --global user.email "misaraty@163.com"
|
2 新建远程仓库
Github网站右上角头像旁+号new repository
。进入项目主页,clone
https://github.com/misaraty/scripts.git
。
3 新建本地仓库
在D:\anzhuang\git
下打开Git Bash Here
,
1
2
|
git init
git clone https://github.com/misaraty/scripts.git
|
4 推送
1
2
3
|
git add -A
git commit -m "update"
git push
|
5 本地同步
6 一键操作
修改C:\Program Files\Git\etc\profile.d\aliases.sh
,
1
2
3
4
|
alias gp='git pull'
alias ga='git add -A && git commit -m "update" && git push'
alias gc1='git checkout --orphan latest && git add -A && git commit -am "update" && git branch -D master && git branch -m master && git push -f origin master'
alias gc2='git checkout --orphan latest && git add -A && git commit -am "update" && git branch -D main && git branch -m main && git push -f origin main'
|
报错
1
2
3
4
5
6
|
There is no tracking information for the current branch.
Please specify which branch you want to merge with.
See git-pull(1) for details.
git pull <remote> <branch>
If you wish to set tracking information for this branch you can do so with:
git branch --set-upstream-to=origin/<branch> master
|
解决
1
|
git branch --set-upstream master origin/master
|
报错
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
lenovo@mi MINGW64 /c/Git/scripts (master)
$ ga
[master c0df6848] update
1 file changed, 1 deletion(-)
fatal: The current branch master has no upstream branch.
To push the current branch and set the remote as upstream, use
git push --set-upstream origin master
To have this happen automatically for branches without a tracking
upstream, see 'push.autoSetupRemote' in 'git help config'.
lenovo@mi MINGW64 /c/Git/scripts (master)
$ git push --set-upstream origin master
To https://github.com/misaraty/scripts.git
! [rejected] master -> master (non-fast-forward)
error: failed to push some refs to 'https://github.com/misaraty/scripts.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
|
解决
1
2
3
4
5
|
lenovo@mi MINGW64 /c/Git/scripts (master)
$ git pull origin master --allow-unrelated-histories
lenovo@mi MINGW64 /c/Git/scripts (master|MERGING)
$ ga
|
报错
每次git push时需要输密码
解决
1
|
git config --global credential.helper store
|
报错
1
2
3
4
|
On branch master
Your branch is ahead of 'origin/master' by 2 commits.
(use "git push" to publish your local commits)
nothing to commit, working tree clean
|
解决
1
2
|
git push -u origin master #本地覆盖远程
git reset --hard origin/master #远程覆盖本地
|
7 代理
1
2
3
4
|
git config --global http.proxy 'socks5://127.0.0.1:7890'
git config --global https.proxy 'socks5://127.0.0.1:7890'
git config --global http.proxy 'http://127.0.0.1:7890'
git config --global https.proxy 'https://127.0.0.1:7890'
|
1
2
|
git config --global --unset http.proxy
git config --global --unset https.proxy
|
1
2
|
git config --global --get http.proxy
git config --global --get https.proxy
|
报错
1
|
Unsupported proxy syntax in '127.0.0.1:7890'
|
解决
打开.gitconfig
文件,去掉单引号,
1
2
3
4
5
6
|
[http]
sslBackend = openssl
sslCAInfo = C:\\Program Files\\Git\\mingw64\\ssl\\cert.pem
proxy = socks5://127.0.0.1:7890
[https]
proxy = socks5://127.0.0.1:7890
|
报错
1
2
3
|
$ git clone https://github.com/misaraty/xxx.git
Cloning into 'xxx'...
fatal: ServicePointManager 不支持具有 socks5 方案的代理。
|
解决
1
2
|
git config --global http.proxy 'http://127.0.0.1:7890'
git config --global https.proxy 'https://127.0.0.1:7890'
|
8 代码行数统计
1
|
git log --author="misaraty" --pretty=tformat: --numstat | awk '{ add += $1; subs += $2; loc += $1 - $2 } END { printf "added lines: %s, removed lines: %s, total lines: %s\n", add, subs, loc }' -
|
9 文件夹索引出错
1
2
3
|
# 比如本地修改了文件夹大小写,远程推送不成功,则可清理整个索引然后重新添加所有文件
git rm -r --cached .
git add .
|