git机制
配置Git
- 正如你在 起步 中看到的,可以用 git config 配置 Git。 首先要做的事情就是设置你的名字和邮件地址:
$ git config --global user.name "John Doe"
$ git config --global user.email johndoe@example.com
- 首先,快速回忆下:Git 使用一系列配置文件来保存你自定义的行为。 它首先会查找系统级的 /etc/gitconfig 文件,该文件含有系统里每位用户及他们所拥有的仓库的配置值。 如果你传递 --system 选项给 git config,它就会读写该文件。
- 接下来 Git 会查找每个用户的 ~/.gitconfig 文件(或者 ~/.config/git/config 文件)。 你可以传递 --global 选项让 Git 读写该文件。
- 最后 Git 会查找你正在操作的仓库所对应的 Git 目录下的配置文件(.git/config)。 这个文件中的值只对该仓库有效,它对应于向 git config 传递 --local 选项。
- 以上三个层次中每层的配置(系统、全局、本地)都会覆盖掉上一层次的配置,所以 .git/config 中的值会覆盖掉 /etc/gitconfig 中所对应的值。
Command
查看配置
$ git config --list
- 简写: git config -l
- git config --list --global
- location:
- /etc/gitconfig(系统)–为全部用户配置
- ~/.gitconfig(全局)–为单个用户配置
- .git/config(本地)–为单个存储库配置
- location:
查看提交状态
$ git status
添加到暂存区、本地仓库
# . 代表提交当前目录下的所有unstage文件除去ignore文件
$ git add .
$ git commit -m "message" #简短提交信息
$ git commit #在文件中编辑提交信息
$ git commit -a -m "message" #合并add,commit操作
或者
$ git commit -am "message"
查看提交日志
git log
git log --oneline #简要版
推送远程仓库(false)
第一种:
#没有设置远程地址
$ git push
fatal: No configured push destination.
Either specify the URL from the command-line or configure a remote repository using
git remote add <name> <url>
and then push using the remote name
git push <name>
第二种:
#设置了远程地址,但是推送时没有加选项
$ git push
fatal: The current branch main has no upstream branch.
To push the current branch and set the remote as upstream, use
git push --set-upstream mommy main
To have this happen automatically for branches without a tracking
upstream, see 'push.autoSetupRemote' in 'git help config'.
解决方案:#设置远程仓库地址
设置远程仓库地址
#随便起一个仓库链接别名mommy
$ git remote add mommy https://github.com/hdb123456/Mommy.git
$ git push mommy main
- 查看远程仓库设置
$ git romote 或git remote -v
代理问题
$ git pull mommy main
fatal: unable to access 'https://github.com/hdb123456/Mommy.git/': Failed to connect to github.com port 443 after 21117 ms: Couldn't connect to server
fatal: unable to access 'https://github.com/xxx/autowrite.git/':
Failed to connect to github.com port 443: Timed out
fatal: unable to access 'https://github.com/xxx/autowrite.git/':
OpenSSL SSL_read: Connection was reset, errno 10054
解决方案:#取消代理
取消代理
#产生原因:git代理配置问题或网速问题
#取消http代理
$ git config --global --unset http.proxy
#取消https代理
$ git config --global --unset https.proxy
仓库内容未同步
$ git push mommy main
info: please complete authentication in your browser...
To https://github.com/hdb123456/Mommy.git
! [rejected] main -> main (non-fast-forward)
error: failed to push some refs to 'https://github.com/hdb123456/Mommy.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. If you want to integrate the remote changes,
hint: use 'git pull' before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
解决方案:变基
commit未同步
$ git pull mommy main
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), 862 bytes | 123.00 KiB/s, done.
From https://github.com/hdb123456/Mommy
* branch main -> FETCH_HEAD
* [new branch] main -> mommy/main
fatal: refusing to merge unrelated histories
解决方案:变基
变基
$ git pull --rebase mommy main
From https://github.com/hdb123456/Mommy
* branch main -> FETCH_HEAD
Successfully rebased and updated refs/heads/main.
推送成功
$ git push mommy main
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 8 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 299 bytes | 299.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0 (from 0)
To https://github.com/hdb123456/Mommy.git
cee7e03..7007e68 main -> main
.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0 (from 0)
To https://github.com/hdb123456/Mommy.git
cee7e03..7007e68 main -> main