1. 程式人生 > >回滾、檢視、合併、同步命令收藏

回滾、檢視、合併、同步命令收藏

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 [email protected]: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