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

本文介绍了Git的基本操作,包括如何回滚远程分支、合并指定提交、同步原始仓库到Fork仓库等,并提供了自动化同步脚本示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.youkuaiyun.com/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
### 如何在 IntelliJ IDEA 中使用 Git 回滚合并分支之前 当遇到需要回滚合并分支之前的情况时,可以按照如下方式操作: #### 使用 Reset 命令撤销更改 如果希望撤消尚未推送到远程仓库的本地更改,可以在IDEA中通过图形界面完成此过程。首先找到想要恢复的提交记录,右键单击该条目并选择`Reset Current Branch to Here...`选项[^1]。 对于那些已经推送至远端服务器上的变更,则应当更加谨慎处理。此时推荐采用混合模式(`--mixed`)执行重置动作,这会保留工作目录中的文件不变而仅改变暂存区状态;或者软模式(`--soft`)来保持所有改动处于已修改但未加入索引的状态[^2]。 #### 执行强制推送 (需慎重考虑) 需要注意的是,在某些情况下可能不得不借助于带有 `-f` 参数的 `push` 指令强行覆盖远程版本库的历史记录。然而这种做法通常只适用于非常特殊的情形,并且可能会带来潜在风险,因此务必提前做好沟通协调以及备份措施后再做决定[^3]。 ```bash git push -f ``` 上述命令用于将当前分支的最新状态强制定向同步给指定名称的目标分支(此处假设为主干),从而实现历史记录的替换效果。不过再次强调,除非绝对必要否则应尽量避免采取此类激进手段。 #### 处理冲突后的回退策略 面对因合并产生的分歧问题,除了直接放弃此次集成尝试外还可以探索其他可能性比如创建新的特性分支继续完善直至满足条件再重新发起请求等更为稳妥的办法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值