`

GIT使用批处理完成日常代码管理

阅读更多

本文默认读者的本机是已经安装好了GIT本地端,因此对于GIT的安装说明,此处不再说明。

 

一般情况下,我们日常对GIT的使用过程中常用的步骤和指令如下:

 

 

 

1、获取代码,执行命令如下:git clone https://github.com/***/***.git执行结果如下:

Administrator@5NYFP69N5F3TA1K MINGW64 /d/git_repository

$ git clone https://github.com/***/***.git

Cloning into '*****'...

remote: Counting objects: 5, done.

remote: Compressing objects: 100% (5/5), done.

remote: Total 5 (delta 0), reused 0 (delta 0), pack-reused 0

Unpacking objects: 100% (5/5), done.

Checking connectivity... done.

 

Administrator@5NYFP69N5F3TA1K MINGW64 /d/git_repository
 

说明:通过上面的动作,我们获取了一个用来放置代码的空间

 

 

 

 

 

 

2、首先查看当前有没有配置过用户:git config --global --list

Administrator@5NYFP69N5F3TA1K MINGW64 /d/git_repository

$ git config --global --list

fatal: unable to read config file 'C:/Users/Administrator/.gitconfig': No such file or directory

 

Administrator@5NYFP69N5F3TA1K MINGW64 /d/git_repository
 

如果出现上面的内容,则说明,我们本地没有任何用户配置信息,需要设置本地用户信息,方便后面使用

 

 

 

 

3、生成ssh-key

Administrator@5NYFP69N5F3TA1K MINGW64 /d/git_repository

$ ssh-keygen -t rsa -C  "****@126.com"

Generating public/private rsa key pair.

Enter file in which to save the key (/c/Users/Administrator/.ssh/id_rsa):

Created directory '/c/Users/Administrator/.ssh'.

Enter passphrase (empty for no passphrase):

Enter same passphrase again:

Your identification has been saved in /c/Users/Administrator/.ssh/id_rsa.

Your public key has been saved in /c/Users/Administrator/.ssh/id_rsa.pub.

The key fingerprint is:

SHA256:qTc89oYQQVTCLv5Hw ***@126.com

The key's randomart image is:

+---[RSA 2048]----+

|         ++=#&BBo|
………………

|   . +           |

|    o            |

+----[SHA256]-----+

 

Administrator@5NYFP69N5F3TA1K MINGW64 /d/git_repository
 
 

4、执行上面操作后会在“C:\Users\Administrator\.ssh”路径下生成一个“id_rsa.pub”文件,用文本编辑工具打开该文件就可以看到生成好的ssh keys,直接复制粘贴到GitHub上(地址:https://github.com/settings/keys)。复制内容如下:

ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAA1puipck4BiFdKqKWf5nPBJZcPjcKVk9W5+npxrliJ/qLJO7DDfR1zbGcSBxy5i9Gm8HUKkDmz9nUt56jxrEaRLlMzJyANM19X/q5qlkHVNXON5Y+e5Wf *****@126.com
 

顺带测试下配置是否成功了:

Administrator@5NYFP69N5F3TA1K MINGW64 /d/git_repository (master)

$ ssh -T git@github.com

The authenticity of host 'github.com (192.30.253.112)' can't be established.

RSA key fingerprint is SHA256:nTomTxdCARLviKw6E5SY8.

Are you sure you want to continue connecting (yes/no)? y

Please type 'yes' or 'no': yes

Warning: Permanently added 'github.com,192.30.253.112' (RSA) to the list of known hosts.

Enter passphrase for key '/c/Users/Administrator/.ssh/id_rsa':

Hi *********! You've successfully authenticated, but GitHub does not provide shell access.

 

Administrator@5NYFP69N5F3TA1K MINGW64 /d/git_repository (master)
 
 

5、由于git是分布式的,每次提交代码其实所有的人自己的电脑上都有一个仓库,因此提交的过程其实是往自己本地仓库提交的过程,接下来我们开始处理本地仓库

 

首先初始化本地仓库(执行结果如下):

Administrator@5NYFP69N5F3TA1K MINGW64 /d/git_repository

$ git init

Initialized empty Git repository in D:/git_repository/.git/

 

Administrator@5NYFP69N5F3TA1K MINGW64 /d/git_repository (master)
 

说明:可以看到,本地的目录已经变成为一个master

 

添加远端仓库:

Administrator@5NYFP69N5F3TA1K MINGW64 /d/git_repository (master)

$ git remote add upstream https://github.com/***/***.git

 

Administrator@5NYFP69N5F3TA1K MINGW64 /d/git_repository (master)
 
 

6、切换路径到待提交代码的测试工程路径下,并执行一下命令:

Administrator@5NYFP69N5F3TA1K MINGW64 /d/git_repository (master)

$ cd ******/

 

Administrator@5NYFP69N5F3TA1K MINGW64 /d/git_repository/******** (master)

$ git add .

warning: LF will be replaced by CRLF in **********/pom.xml.

The file will have its original line endings in your working directory.
 

说明:当看到“The file will have its original line endings in your working directory.说明当前目录下所有文件均已提交。

 

7、提交代码到本地仓库

Administrator@5NYFP69N5F3TA1K MINGW64 /d/git_repository/****** (master)

$ git commit -am 'add readme file '

[master 3357496] add readme file

 1 file changed, 1 insertion(+), 1 deletion(-)

 

Administrator@5NYFP69N5F3TA1K MINGW64 /d/git_repository/****** (master)
 

说明:看到上面的信息,说明本地文件提交成功

 

 

 

8、将代码推送到远端服务器

Administrator@5NYFP69N5F3TA1K MINGW64 /d/git_repository/****** (master)

$ git push origin master

Counting objects: 511, done.

Delta compression using up to 8 threads.

Compressing objects: 100% (449/449), done.

Writing objects: 100% (511/511), 1.63 MiB | 5.00 KiB/s, done.

Total 511 (delta 115), reused 0 (delta 0)

remote: Resolving deltas: 100% (115/115), done.

To https://github.com/******/******.git

   e17020f..3357496  master -> master

 

Administrator@5NYFP69N5F3TA1K MINGW64 /d/git_repository/******* (master)
 

说明:看到上面的信息,说明代码提交成功,这时,你可以到git的代码库中确认,代码是否已经提交上去。

 

想想每次都这么玩,好像不是职业懒人应该有的风格,为了简化后面日常代码开发结束后令人牙痒的操作,我们写一个简单的bat批处理,把日常这些琐事交给他来做,这里就抛砖引玉一下:

 

title GIT提交批处理——魏小敏
color 16
@echo off
echo *************************************************************
echo *                GIT 代码提交批处理                         *
echo *  Author: 魏小敏                                           *
echo *  Version: 1.0                                             *
echo *  Created: 2017/3/26                                       *
echo *  Last Updated by: 魏小敏                                  *
echo *************************************************************

echo;
echo;

echo 开始提交代码到本地仓库
echo 当前目录是:%cd%

echo;
echo;
echo 开始识别当前git的版本
echo ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
git --version
echo ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
echo;
echo;

echo 开始添加变更
echo ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
git add -A .
echo 执行结束!
echo ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

echo;
echo 提交变更到本地仓库
echo ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
git commit -m "修改代码"
echo ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

echo;
echo 将变更情况提交到远程git服务器
echo ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
git push origin master
echo ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

echo;
echo 批处理执行完毕!
echo;

pause

 

 接下来要做的事情就是将这个批处理保存成bat文件保存到你本地工程的根路径下,每次功能开发结束后直接双击,完成一连串动作,是不是很爽!

 

当然,上面这个版本不支持输入提交代码时的注释,对有些注释控可能不适合,我们提供另外一版支持用户个性化输入代码提交备注信息的批处理,以便满足各种人群的各种口味!

title GIT提交批处理——魏小敏
color 16
@echo off
echo *************************************************************
echo *                GIT 代码提交批处理                         *
echo *  Author: 魏小敏                                           *
echo *  Version: 1.0                                             *
echo *  Created: 2017/3/26                                       *
echo *  Last Updated by: 魏小敏                                  *
echo *************************************************************

echo;
echo;

echo 开始提交代码到本地仓库
echo 当前目录是:%cd%

echo;
echo;
echo 开始识别当前git的版本
echo ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
git --version
echo ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
echo;
echo;

echo 开始添加变更
echo ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
git add -A .
echo 执行结束!
echo ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

echo;
echo;
echo;
echo;
echo;
echo;
echo;
echo;
echo 提交变更到本地仓库
echo ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
@echo off
set /p var=请输入提交说明: 
echo;
echo 您输入的提交说明为:  %var%
echo;
git commit -m %var%
echo ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

echo;
echo;
echo;
echo;
echo;
echo;
echo;
echo;
echo 将变更情况提交到远程git服务器
echo ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
git push origin master
echo ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

echo;
echo 批处理执行完毕!
echo;

pause

 

在这个版本的基础上,其实你还可以继续演进下去,不要吝啬,有好的想法贴出来一起分享哦 ^_^

 

 各位看官,原创不易啊,转载请注明出处: http://danlley.iteye.com 看在打字不易的份上,打赏一个吧

 

参考资料:

http://blog.csdn.net/hustpzb/article/details/8230454/
http://www.cnblogs.com/wuzhiyi/p/4645556.html
http://jingyan.baidu.com/article/a65957f4e91ccf24e77f9b11.html

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics