目录

GitHub

misaraty 更新 | 2024-09-15
前言
自己大部分代码都托管到Github上的PublicPrivate仓库上。

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,

SettingsSSH and GPG keysNew SSH keys,输入key。在Git Bash中,

1
ssh -T git@github.com

输入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
注意
  • 执行上述命令时,需在对应的文件夹下,如D:\anzhuang\git\scripts

  • 上述命令写法,考虑到了删除文件后的推送问题,即删除文件后推送依旧有效。

5 本地同步

1
git pull

6 一键操作

修改C:\Program Files\Git\etc\profile.d\aliases.sh1

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
git config --list
  • 配置代理
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 .

  1. how to delete all commit history in github? ↩︎