Shell:自动无密码提交git, cron git push with ssh key, 无密码提交git 到 Bitbucket或者GitHub

定时任务:

0 0 * * * /home/username/Sites/git/repo/commit.sh

commit.sh 文件

#!/usr/bin/env bash

GIT=`which git`
REPO_DIR=/var/www/html/
cd ${REPO_DIR}
${GIT} add -u .
${GIT} commit -a -m "Daily Crontab Backup at "$(date "+%Y-%m-%d %H:%M:%S")
${GIT} push -u origin master

 

实现步骤

1.  生成密钥:

cd ~/.ssh/                 #Your ssh directory
ssh-keygen -t rsa -f my_rsa -C "youraccount@gmail.com"    #Press enter for all values

或者参看文档: https://confluence.atlassian.com/bitbucket/set-up-an-ssh-key-728138079.html#SetupanSSHkey-ssh1

2. 复制公钥到Bitbucket或者GitHub

cat id_rsa.pub
Shell:自动无密码提交git, cron git push with ssh key, 无密码提交git 到 Bitbucket或者GitHub
Shell:自动无密码提交git, cron git push with ssh key, 无密码提交git 到 Bitbucket或者GitHub

A: 如果出现询问密码的提示

Password for ‘https://youraccount@bitbucket.org’:

那么执行命令

git remote -v

看看你远程提交的地址是不是 HTTPS 的方式,而这种方式一般是要讯问密码的。我们需要采用 SSH 方式来代替 HTTPS 方式

git remote -v
origin  https://github.com/USERNAME/REPOSITORY.git (fetch)
origin  https://github.com/USERNAME/REPOSITORY.git (push)

修改

git remote set-url origin git@github.com:USERNAME/REPOSITORY.git

验证一下

git remote -v
# Verify new remote URL
origin  git@github.com:USERNAME/REPOSITORY.git (fetch)
origin  git@github.com:USERNAME/REPOSITORY.git (push)

当然,你也可以直接修改项目根目录下 .git/config 文件

cat /var/www/html/.git/config 
[core]
	repositoryformatversion = 0
	filemode = true
	bare = false
	logallrefupdates = true
[remote "origin"]
	url = https://github.com/USERNAME/REPOSITORY.git
	fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
	remote = origin
	merge = refs/heads/master
[user]
	email = USERNAME@gmail.com
	name = USERNAME

直接修改 [remote “origin”] 下面的 url 就可以了!

 

B: 如果出现提示 “Warning: Permanently added the RSA host key for IP address ‘207.***.***.***’ to the list of known hosts.”

类似

Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.

或者

Warning: Permanently added the RSA host key for IP address '207.***.***.***' to the list of known hosts.
Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.

解决方案:

  1. In the correct location
  2. Copied correctly to Bitbucket
  3. In the ssh-agent
    ssh-add -l
  4. 创建~/.ssh/config文件 (That you have your ~/.ssh/config file setup)
    Host bitbucket.org
    IdentityFile ~/.ssh/my_rsa
  5. ssh链接仓库 (You can connect via SSH directly)
    ssh -Tvv git@bitbucket.org
  6. And that your /path/to/repo/.git/config has the SSH URL in it correctly.
 OK 至此,应该没有什么问题了!

 

另外多说一句,提交版本的时候,我们没有必要全部上传,那样会增加版本库的体积,因为一般github或者bitbucket都是有容量限制的,1G 或者 2G之类的,所以考虑一下区别:

git add [<路径>]: 把所有路径下的文件加入到索引。

git add -u [<路径>]: 把<路径>中所有跟踪(tracked)文件中被修改过或已删除文件的信息添加到索引库。它不会处理未跟踪(untracked)的文件。省略<路径>表示:即当前目录。

git add -a [<路径>]: 表示把<路径>中所有跟踪(tracked)文件中被修改过或已删除文件和所有未跟踪(untracked)的文件信息添加到索引库。省略<路径>表示:即当前目录。

git add -i [<路径>]: 命令查看<路径>中被所有修改过或已删除文件但没有提交的文件。

git commit -m “代码提交信息”: 提交暂存区到仓库区

git commit -a -m “代码提交信息”: 提交工作区自上次commit之后的变化,直接到仓库区

Git 常用命令速查表

创建版本库

$ git clone <url>                  #克隆远程版本库
$ git init                         #初始化本地版本库

修改和提交

$ git status                       #查看状态
$ git diff                         #查看变更内容
$ git add .                        #跟踪所有改动过的文件
$ git add <file>                   #跟踪指定的文件
$ git mv <old><new>                #文件改名
$ git rm<file>                     #删除文件
$ git rm --cached<file>            #停止跟踪文件但不删除
$ git commit -m "commit messages"  #提交所有更新过的文件
$ git commit --amend               #修改最后一次改动

查看提交历史

$ git log                    #查看提交历史
$ git log -p <file>          #查看指定文件的提交历史
$ git blame <file>           #以列表方式查看指定文件的提交历史

撤销

$ git reset --hard HEAD      #撤销工作目录中所有未提交文件的修改内容
$ git checkout HEAD <file>   #撤销指定的未提交文件的修改内容
$ git revert <commit>        #撤销指定的提交
$ git log --before="1 days"  #退回到之前1天的版本

分支与标签

$ git branch                   #显示所有本地分支
$ git checkout <branch/tag>    #切换到指定分支和标签
$ git branch <new-branch>      #创建新分支
$ git branch -d <branch>       #删除本地分支
$ git tag                      #列出所有本地标签
$ git tag <tagname>            #基于最新提交创建标签
$ git tag -d <tagname>         #删除标签

合并与衍合

$ git merge <branch>        #合并指定分支到当前分支
$ git rebase <branch>       #衍合指定分支到当前分支

远程操作

$ git remote -v                   #查看远程版本库信息
$ git remote show <remote>        #查看指定远程版本库信息
$ git remote add <remote> <url>   #添加远程版本库
$ git fetch <remote>              #从远程库获取代码
$ git pull <remote> <branch>      #下载代码及快速合并
$ git push <remote> <branch>      #上传代码及快速合并
$ git push <remote> :<branch/tag-name>  #删除远程分支或标签
$ git push --tags                       #上传所有标签

更多内容请查看 Git 文档

 

本文:Shell:自动无密码提交git, cron git push with ssh key, 无密码提交git 到 bitbucket或者GitHub

Loading

One Comment

Add a Comment

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.