git 命令 - 回滚、查看、合并、同步命令收藏

1 git 回滚

强制回滚远程分支
git push -f origin commit_id:master

强制放弃本地修改
git reset --hard

2 git 合并某一个commit id提交的内容

git cherry-pick 62ecb3

3 git 查看远程仓库地址

git remote -v

4 Git同步原始仓库和Fork仓库

步骤
初始化本地仓库
mkdir test-repo
cd test-repo
git init
添加远程仓库地址
添加原始仓库地址,就是被Fork的。

git remote add parent https://github.com/user/test-repo.git
地址是https协议的,不能是ssh协议的,除非有权限。

添加自己远程仓库地址,最好是ssh协议地址。

git remote add origin git@github.com:SeayXu/aspnetcore-doc-cn.git

拉取原始远程仓库到本地
git pull parent master注意:
初始化的仓库默认分支是master,如果你同步下来的分支不是在master分支,需要切换到其他的分支时,需要先提交一下本地仓库,然后再切换。

提交本地仓库
在拉取原始仓库后,可以根据自己需要是否需要本操作。如果拉取后有改动,执行提交操作,否则直接下一步。
git add -A

git commit -m "updated at:$(date '+%Y-%m-%d %H:%M:%S')"
这里为了自动化,后面的提交信息是一串时间。

推送本地仓库到远程仓库

git push origin dev
脚本
为了能不每次都敲这么多命令,可以将这些命令写在shell脚本中。
下面是我的一个示例:

sync.sh

#!/bin/bash
echo "change dir..."
cd ../src
echo "dir:`pwd`"

echo -e '\n'

echo "git pull repo from parent..."
git pull parent master
echo "git pull repo from parent complated!"

echo -e '\n'

echo "git commit repo into local..."
git add -A
git commit -m "updated at:$(date '+%Y-%m-%d %H:%M:%S')"
echo "git commit repo into local complated!"

echo -e '\n'
echo "git push repo to origin...!"
git push origin master
echo "git push repo to origin complated!"


5 git合并其他fork分支到本地分支

1) To pull in somebody else's changes, first add a remote that points to their repository. For example:

git remote add soniakeys https://github.com/soniakeys/goptimize.git

Then, you can fetch those changes into your repository (this doesn't change your code, yet):

git fetch soniakeys

Finally, to merge those changes, make sure you're on your master branch and:

git merge soniakeys/master

2) To be polite, you would normally ask the author whether it's okay to pull the changes. Just because they're on a public repository doesn't necessarily mean they are ready to pull. There might be further work to do, or perhaps intellectual property issues, or whatever. However, with published changes on an open source repository, asking is not strictly required.


6 打patch

diff -Naur 旧的目录 新的目录 > patch文件


[2] Git合并特定commits 到另一个分支
http://blog.csdn.net/ybdesire/article/details/42145597

[3] Git同步原始仓库到Fork仓库中

https://www.linuxidc.com/Linux/2016-06/132354.htm

[4] 用Diff和Patch工具维护源码, https://www.ibm.com/developerworks/cn/linux/l-diffp/

git 资料

GitHub 使用教程图文详解  http://www.linuxidc.com/Linux/2014-09/106230.htm
Git 标签管理详解 http://www.linuxidc.com/Linux/2014-09/106231.htm
Git 分支管理详解 http://www.linuxidc.com/Linux/2014-09/106232.htm 
Git 远程仓库详解 http://www.linuxidc.com/Linux/2014-09/106233.htm
Git 本地仓库(Repository)详解 http://www.linuxidc.com/Linux/2014-09/106234.htm
Git 服务器搭建与客户端安装  http://www.linuxidc.com/Linux/2014-05/101830.htm
Git 概述 http://www.linuxidc.com/Linux/2014-05/101829.htm
分享实用的GitHub 使用教程 http://www.linuxidc.com/Linux/2014-04/100556.htm
Ubuntu下Git服务器的搭建与使用指南  http://www.linuxidc.com/Linux/2015-07/120617.htm
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值