markdown git从本地推送到远程服务器

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了markdown git从本地推送到远程服务器相关的知识,希望对你有一定的参考价值。

## 服务器创建项目文件夹并初始化git
```
# 创建项目文件(要被推送的项目)
mkdir /usr/share/nginx/git_dir
cd /usr/share/nginx/git_dir

# 初始化 git 并配置 git 可以 git push 到远程更新工作树
git init
git config receive.denyCurrentBranch updateInstead
```

## 本地添加远程地址
把本地的 `~/.ssh/id_rsa.pub` 的内容导入到远程服务器的 `~/.ssh/authorized_keys` 里(允许本地直接连接 git)
```
#复制 id_rsa.pub 内容
cat ~/.ssh/id_rsa.pub
#导入到服务器 ~/.ssh/authorized_keys 中
sudo vi ~/.ssh/authorized_keys
```
本地添加远程 git 地址
```
git remote add server ssh://ubuntu@xxx.xxx.xxx.xxx/usr/share/nginx/git_dir
```
## 本地可以推送到远程了
```
git push server master
```



## QA:
### 如果 git push server master 出错
```
$ git push server master
fatal: bad numeric config value 'updateInstead' for 'receive.denycurrentbranch' in config: invalid unit
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
```
请更新 git
```
sudo add-apt-repository ppa:git-core/ppa
sudo apt-get update
sudo apt-get install -y git
```

---

### 错误2(远程初始化不为空的文件夹时,报错如下)
> [Git push error '[remote rejected] master -> master (branch is currently checked out)'](https://stackoverflow.com/questions/2816369/git-push-error-remote-rejected-master-master-branch-is-currently-checked)

```
$ git push server master -f
Counting objects: 5400, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (2396/2396), done.
Writing objects: 100% (5400/5400), 6.76 MiB | 317.00 KiB/s, done.
Total 5400 (delta 2734), reused 5237 (delta 2625)
error: Untracked working tree file '.editorconfig' would be overwritten by merge.
remote: Resolving deltas: 100% (2734/2734), done.
To ssh://192.168.xxx.xxx/usr/share/nginx/git_dir
 ! [remote rejected] master -> master (Could not update working tree to new HEAD)
error: failed to push some refs to 'ssh://ubuntu@192.168.xxx.xxx/usr/share/nginx/git_dir'
```

处理方法:
检查目标项目中的.git / config:
```
$ cat .git/config 
[core]
    repositoryformatversion = 0
    filemode = true
    bare = false
    logallrefupdates = true
[receive]
    denyCurrentBranch = updateInstead
```
如果 core.bare 为 false,你可以将其设置为 true
```
$ git config core.bare true
```
本地再次推送到远程
```
git push server master
```
远程查看最新 log
```
$ git log -1
commit 0623b1b900ef7331b9184722a5381bbdd2d935ba
Author: aircraft < aircraft_xxx@126.com>
Date:   Thu May 17 21:54:37 2018 +0800
and now you can not use git in your "workspace":
```
现在你不能在你的“工作区”使用git 命令:
```
$ git status
fatal: This operation must be run in a work tree
you should set bare.bare back to false.
```
还原 core.bare 为 false
```
$ git config core.bare false
```
使用 git reset HEAD 到HEAD log 位置
```
$ git reset --hard HEAD
```


以上是关于markdown git从本地推送到远程服务器的主要内容,如果未能解决你的问题,请参考以下文章

如何从本地 Windows 存储库推送到 Windows git 远程服务器

在自己的服务器上搭建git仓库服务器(本地推送到远程仓库远程仓库推送到本地)

markdown 从本地分支推送到另一个远程分支(如果不退出则在远程创建新分支)

markdown 从本地分支推送到另一个远程分支(如果不退出则在远程创建新分支)

git 本地仓库同时推送到多个远程仓库

如何将本地更改推送到 Bitbucket 上的远程 Git 存储库