您的位置:首页 > 其它

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

2018-02-07 13:25 239 查看

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文件

[1] Rolling back a remote Git repositoryhttps://stackoverflow.com/questions/588414/rolling-back-a-remote-git-repository[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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: