git仓库使用相关

土豆条
土豆条 2016-9-3

1、迁移原git仓库

先查看remote的名字

git branch -r

假设你的remote是origin,用git remote set_url 更换地址

git remote set-url origin remote_git_address

remote_git_address更换成你的新的仓库地址


如果你想从别的 Git 托管服务那里复制一份源代码到新的 Git 托管服务器上的话,可以通过以下步骤来操作。

1). 从原地址克隆一份裸版本库,比如原本托管于 GitHub。

git clone --bare git://github.com/username/project.git

2). 然后到新的 Git 服务器上创建一个新项目,比如 GitCafe。

3). 以镜像推送的方式上传代码到 GitCafe 服务器上。

cd project.git

git push --mirror git@gitcafe.com/username/newproject.git

4). 删除本地代码

cd ..

rm -rf project.git

5). 到新服务器 GitCafe 上找到 Clone 地址,直接 Clone 到本地就可以了。

git clone git@gitcafe.com/username/newproject.git

这种方式可以保留原版本库中的所有内容。


2、Git Pull 避免用户名和密码方法

进入git bash终端 添加 Git Config内容, 输入如下命令:

git config --global credential.helper store

执行完后查看%HOME%目录下的.gitconfig文件,会多了一项。

[credential]
helper = store

重新开启git bash会发现git push时不用再输入用户名和密码。

注意:

    如果不能提交代码到git服务器,需要将帐号绑定到git工具上。若不绑定,当你执行git commit  是报如下错误。

*** Please tell me who you are.  
git config --global user.email "you@example.com"  
git config --global user.name "Your Name"    
to set your account's default identity.  
Omit --global to set the identity only in this repository.

执行 git config --global 命令,全局配置用户的姓名和邮箱,就可以提交git 代码了

git config  --global user.email {用户邮箱}
git config --global user.name {用户账号}


3、git 如何恢复到指定版本

1. 查看git的提交版本和id 拿到需要恢复的版本号 

git log 

2. 恢复到指定版本 

git reset --hard 42ce2c5fbda7d7cfef74ec33674a5198a8776e4a //版本id

3. 强制push

git push -f origin master


4、git - 提供带有密码的 git URL,包括 "@"符号 标签 git


用户名或密码中含有像@这样的特殊符号,而不能被正常解析

例如:

git clone https://[username]:[pass@word]@/remote

采用%40 代替 @

git clone https://[username]:[pass%40word]@/remote


5、GIT导出文件操作

# GIT命令导出文件更新压缩包

git archive --format=zip -o ./代码文件更新压缩包.zip 分支名称 $(git diff --name-only 提交记录ID1 提交记录ID2)
git archive --format=zip -o ./代码文件更新压缩包.zip develop $(git diff --name-only 5872f5d12eee65672349ab0c4b08ae443a808b80 f2d1ab0f6571448d33c1c12871c9f7200ba506a5)




6、GIT 统计相关命令


统计个人代码量

git log --author="dzmvc" --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 }' -

统计每个人增删行数

git log --format='%aN' | sort -u | while read name; do echo -en "$name\t"; git log --author="$name" --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 }' -; done

贡献值统计

git log --pretty='%aN' | sort -u | wc -l

查看排名前 5 的贡献者

git log --pretty='%aN' | sort | uniq -c | sort -k1 -n -r | head -n 5

提交数统计

git log --oneline | wc -l

添加或修改的代码行数

git log --stat|perl -ne 'END { print $c } $c += $1 if /(\d+) insertions/'

使用gitstats

GitStats项目,用Python开发的一个工具,通过封装Git命令来实现统计出来代码情况并且生成可浏览的网页。官方文档可以参考这里。

使用方法

git clone git://github.com/hoxu/gitstats.git
cd gitstats

./gitstats 你的项目的位置 生成统计的文件夹位置

可能会提示没有安装gnuplot画图程序,那么需要安装再执行:

//mac osx
brew install gnuplot
//centos linux
yum install gnuplot

生成的统计文件为HTML:


1.jpg


使用cloc

npm install -g cloc


回帖
  • 消灭零回复

微信二维码

微信二维码

微信扫码添加微信好友