Git Guide

2020/05/21 Blog

git实用guide

一、git之https或http方式设置记住用户名和密码的方法

https方式每次都要输入密码,按照如下设置即可输入一次就不用再手输入密码的困扰而且又享受https带来的极速

设置记住密码(默认15分钟):

$git config --global credential.helper cache

如果想自己设置时间,可以这样做:

$git config credential.helper 'cache --timeout=3600'

这样就设置一个小时之后失效

长期存储密码:

$git config --global credential.helper store

重置存储的密码:

$git config --global --unset credential.helper

注意 : --global是针对全局的设置,如果有不同的仓库对应不同的用户名和密码,推荐不要用--global

增加远程地址的时候带上密码也是可以的。(推荐) http://yourname:password@git.oschina.net/name/project.git

补充:使用客户端也可以存储密码的。

如果你正在使用ssh而且想体验https带来的高速,那么你可以这样做: 切换到项目目录下 : cd projectfile/

移除远程ssh方式的仓库地址

$git remote rm origin

增加https远程仓库地址

$git remote add origin http://yourname:password@git.oschina.net/name/project.git

二、配置diff,merge,editor

$git config --global diff.tool p4merge
$git config --global merge.tool p4merge
$git config --global merge.tool core.editor vim
$git config --global core.editor "vim"

三、提交代码

$git status
$git add xxx
$git commit -m "xxx"

四、上传代码

$git push origin HEAD:refs/for/tizen_4.0 (for review.tizen.org)
$git push origin master (for github)

五、撤销提交

$git reset commid_id 保留更改
$git reset --hard commit_id  不保留

六、不生成新的提交

$git status
$git add xxx
$git commit --amend

七、保存本地改动到栈中

$git stash

从栈中恢复保存的改动:

$git stash pop

八、将gerrit的patch拉到本地

$git check

九、查看某个指定文件的提交记录

$git log --pretty=oneline filepath
$git show 命令查看具体的修改详情了
$git show commid_id filepath
$git

十、Git撤销已经提交到gerrit的某个文件

$git reset commid_previous_id file_to_uncommit
$git checkout -- file_to_uncommit
$git commit --amend -s
$git push…

十一、查看某个commit改动了哪些文件并简化显示

$git show <commit-id> --stat

其中 –stat 选项用于简化显示,不展开文件修改细节~

From https://zhidao.baidu.com/question/583706356145608645.html

十二、git改变分支名称

  1. 本地分支重命名(还没有推送到远程)
$git branch -m oldName newName
  1. 远程分支重命名 (已经推送远程-假设本地分支和远程对应分支名称相同)

a. 重命名远程分支对应的本地分支

$git branch -m oldName newName

b. 删除远程分支

$git push --delete origin oldName
or
$git push origin :oldName (删除远程origin中的oldName分支)
or
$git push upstream :oldName (删除远程upstream中的oldName分支)

c. 上传新命名的本地分支

$git push origin newName:newName (往远程origin中传newName分支)
or
$git push upstream newName:newName (远程upstream中传newName分支)

d. 把修改后的本地分支与远程分支关联

$git branch --set-upstream-to origin/newName

From https://blog.csdn.net/weixin_42082222/article/details/88765419

十三、git代码迁移

$git clone --mirror <URL to my OLD repo location>
$cd <New directory where your OLD repo was cloned>
$git remote set-url origin <URL to my NEW repo location>
$git push -f origin

十四、Cherry picking a pull request

For example you want to cherry pick https://github.com/fgrehm/vagrant-cachier/pull/164

• Copy the pull request url, append “.patch” and open it in a browser.

e.g. https://patch-diff.githubusercontent.com/raw/fgrehm/vagrant-cachier/pull/164.patch

• It will automatic resolve as something like below

https://patch-diff.githubusercontent.com/raw/fgrehm/vagrant-cachier/pull/164.patch

• Use the new url for cherry picking the patch on your local repository

$ curl https://patch-diff.githubusercontent.com/raw/fgrehm/vagrant-cachier/pull/164.patch | git am

Cherry picking a commit:

• Get url for the commit

example: https://github.com/LalatenduMohanty/vagrant-cachier/commit/081c809fc369210c6bb06ad9786b8f9be4fb8570

• Append “.patch” and and use it for patch the local code.

$ curl https://github.com/LalatenduMohanty/vagrant-cacrhier/commit/081c809fc369210c6bb06ad9786b8f9be4fb8570.patch | git am

From https://lalatendu.org/2015/12/02/cherry-pick-a-pr-pull-request-from-github/

Search

    欢迎关注我的微信号

    elishateng

    Table of Contents