Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

【GIT】常用GIT知识点 #14

Open
zhongxia245 opened this issue Sep 10, 2016 · 10 comments
Open

【GIT】常用GIT知识点 #14

zhongxia245 opened this issue Sep 10, 2016 · 10 comments
Labels

Comments

@zhongxia245
Copy link
Owner

zhongxia245 commented Sep 10, 2016

  • 获取git仓库源码
git clone 仓库地址
  • 获取git的最新内容
git pull
  • git pull 冲突报错如何解决
//本地修改的内容,先存放起来
git stash  

//更新
git pull

//把刚才本地修改内容,合并进来
git stash pop stash@{0}
  • 放弃本地修改的内容,还原代码
git reset --hard
  • 如何修改GIT远程仓库地址
git remote set-url origin 新地址
@zhongxia245 zhongxia245 changed the title 【GIT】常用知识点 【GIT】常用GIT知识点 Sep 14, 2016
@zhongxia245
Copy link
Owner Author

zhongxia245 commented Sep 24, 2016

如何把本地的git仓库,推送到github仓库(远程仓库)上

//1. 初始化git仓库,在项目根目录下
git init 

//2. 设置远程仓库地址:  origin 可以随便取, 后面是 github 地址
git remote add  origin  https://github.com/zhongxia245/React-Mi-Shop.git

//3. 提交到远程仓库, origin 仓库下的 master分支
git push origin  master 

可能遇到错误

git push -u origin master                                                                                             
To https://github.com/zhongxia245/React-Mi-Shop.git
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'https://github.com/zhongxia245/React-Mi-Shop.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

问题(Non-fast-forward)的出现原因在于:git仓库中已经有一部分代码,所以它不允许你直接把你的代码覆盖上去。于是你有2个选择方式:

强推,即利用强覆盖方式用你本地的代码替代git仓库内的内容

git push -f  origin master 

设置好远程仓库后, 后面的提交, 需要使用

//把本地 origin 仓库, 推送远程 master仓库
git push origin master    

@zhongxia245
Copy link
Owner Author

如何把本地分支提交到远程分支

//本地切换到  zx-demo1 分支,(git 执行的命令,都是表示在当前分支下 起作用的)
git checkout zx-demo1

...

//提交到远程 origin 仓库  的 master 分支上
git push origin master

//查看远程仓库的列表
git remote -v 

//提交到 远程仓库 upstream zx-demo1 分支上
git push upstream zx-demo1

@zhongxia245
Copy link
Owner Author

更新远程分支代码

如果本地分支有 没有 commit 的, git pull 会报错。 让你先把本地的提交了。
那么是否有其他方法呢?

git stash 

git pull 远程仓库  远程分支名

git stash pop stash@{0}

@zhongxia245
Copy link
Owner Author

zhongxia245 commented Dec 2, 2016

git常用命令脑图
原文地址:http://finalshares.com/read-178

图片加载不出来,请点击 地址:http://ww3.sinaimg.cn/large/006y8mN6gw1facwr32dwrj31cv2fue0o.jpg

@zhongxia245
Copy link
Owner Author

拉取远程分支到本地分支或者创建本地新分支

git fetch origin branchname:branchname

可以把远程某各分支拉去到本地的branchname下,如果没有branchname,则会在本地新建branchname

git checkout origin/remoteName -b localName

获取远程分支remoteName 到本地新分支localName,并跳到localName分支

@zhongxia245
Copy link
Owner Author

GIT 合并多个 commit

选择需要合并到哪一个commit

git rebase -i HEAD^    //这里也可以是commit的HEAD 编号

确定合并的方式

# 回车之后,会出现以下内容
# 除了第一个 为 pick ,其他几个pick 全部修改成  squash (其他命令也可以,命令列表看下面提示)
  1  pick 52d5383 添加add的說明 
  2 squash 1838a23 test123
  3 squash f2ac632 test git rebase -i
  4 squash b4b0529 test2

修改新合并成的 commit 备注

# 再次回车,需要填写合并之后 commit 的 备注

# vim 操作自行百度
# vim 操作, 按 i 键,修改 commit的备注内容, 然后 按 ESC键, 再按 shift+:  ,在按 wq 保存退出 , ok

成功

@zhongxia245
Copy link
Owner Author

如何查看指定文件的 提交历史

有时候我们需要查看指定文件,被谁修改过。 如果从 git log 里面一个一个找,那还不累死。

# 查看指定文件的提交记录
git log --pretty=oneline <文件名>

# 查看指定文件在某个版本修改的内容
git show <git提交版本号> <文件名>

@zhongxia245
Copy link
Owner Author

zhongxia245 commented Feb 21, 2017

同一个 commit ,如何提交到两个分支上

# 1. 先提交到一个分支
git commit -am 'commit some content'
git push 

# 2. 查看刚commit 的 id,复制出来
git log 

# 3. 切换到另外一个分支,然后增加刚才的commit 提交内容
git co -b new_branch 

git cherry-pick [commit-id]

git push 
# 4. ok, 在两个分支上都有 最新的commit, 然后就可以提交两个Merge Request 

@zhongxia245 zhongxia245 added the GIT label Sep 6, 2019
@zhongxia245
Copy link
Owner Author

git pull --rebase

@zhongxia245
Copy link
Owner Author

zhongxia245 commented Sep 23, 2020

git revert 后在merge导致代码消失

问题描述:

  1. preonline 拉取出 A分支,然后A开发完,merge 到了 preonline ,这时发现后端有问题,因此前端需要回滚,就把A分支从 preonline revert 掉(避免影响其他同学的需求代码上线)。
  2. 一天后,后端问题修复,A分支需要重新上线, 这个时候把 A分支在合到 preonline ,会发现 A分支的代码不见了。

问题原因

preonline 分支的commit 记录

....some commit => A commit => A merge preonline => A revert => ... some other commit

A 分支commit

....some commit => A commit

这个是把A分支的代码 再次 merge 到 preonline 中,会被后面的 A revert 给『消除掉了』,因为 A revert 的 commit 时间是在 A commit 之后,因此 preonline 会认为 A revert 才是最新的提交结果。

解决方案

直接在 preonline 上找到revert的版本号 revert_version_of_mine, 对这个操作执行 revert

git revert --no-commit revert_version_of_mine

git commit -a -m 'revert revert'

git push

参考文章

  1. git revert 再merge导致代码丢失

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant