Discuz! Board

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 8260|回复: 16
打印 上一主题 下一主题

git命令行操作

[复制链接]

697

主题

1142

帖子

4086

积分

认证用户组

Rank: 5Rank: 5

积分
4086
跳转到指定楼层
楼主
发表于 2017-3-2 15:12:38 | 只看该作者 |只看大图 回帖奖励 |正序浏览 |阅读模式
本帖最后由 java 于 2019-5-28 11:37 编辑

                                
git config --global user.email  "***@xxx.com"
git config --global user.name  "***"

# 配置默认编辑器, 默认是nano, 可以配置为vim 或其他
git config --global core.editor vim

#配置比较工具.可以用git difftool 调用.
git config --global diff.tool meld
git config --global difftool.prompt false




远程路径修改  如邮箱地址大小写变化了。git remote -v查看原来的路径后,
直接修改.git/config文件


$ git status



$ git log

$ git checkout -b "backup"



$ git checkout discover



$ ssh-keygen

(/c/Users/xxx/.ssh/id_rsa)

$ git config -l



Hint: To automatically insert Change-Id, install the hook:
gitdir=$(git rev-parse --git-dir); scp -p -P 29418 xx@192.168.11.56:hooks/commit-msg ${gitdir}/hooks/


.git\hooks\commit-msg

git commit --amend



git push origin HEAD:refs/for/discover
$ git pull --rebase
$ git add sub

$ git submodule update


$ git pull
内容回退
$ git checkout account-duiba/
$ git reset .

添加
git status
git add .

git status

git commit    (vi 编辑)

$ git push origin HEAD:refs/for/discover



对比$ git diff


数据暂存及恢复

$git stash
do some work
$git stash pop


查看 git 提交到本地但未push到远端的代码

git log 本地branch ^远程分支   可以查看本地有远程没有的提交。

git log master ^origin/master


git log 远程分子 ^本地branch   可以查看远程有,本地没有的提交。






查看clone的源码路径
git remote -v




git reset HEAD^1 --hard

git reset --help




git log 有许多选项可以帮助你搜寻感兴趣的提交,接下来我们介绍些最常用的。

我们常用 -p 选项展开显示每次提交的内容差异,用 -2 则仅显示最近的两次更新:
git log -p -2

--stat,仅显示简要的增改行数统计:
git log --stat

git log --pretty=format:"%h %s" --graph


git log -p 431072838ba5ab7b9d28614e6e78b3be6a84145c  (查看具体某次提交的变化)


https://git-scm.com/book/zh/v1/G ... 4%E5%8E%86%E5%8F%B2

本地创建新分支

git checkout -b local/list_sort_0615

==================git-merge
git merge是用来合并两个分支的。
git merge b
      # 将b分支合并到当前分支
--ff命令
--ff是指fast-forward命令。当使用fast-forward模式进行合并时,将不会创造一个新的commit节点。默认情况下,git-merge采用fast-forward模式。
--no-ff命令
即使可以使用fast-forward模式,也要创建一个新的合并节点。这是当git merge在合并一个tag时的默认行为。

git checkout master

git pull

git merge --no-ff origin/develop

git push origin HEAD:refs/for/master

-----------------------

merge

把其它分支上的代码 merge到当前分支
git checkout curbarnch
git merge --no-ff origin/develop

git add .
git commit -m "meger 代码合并"



===================================
测试通过后,确定要上线,需要把feature分支合并到develop中,合并前最好先同develop做一次rebase,尽量把冲突放到当前feature分支解决

git checkout feature_lyl_calendarList_0620

git rebase origin/develop

git checkout develop

git merge --no-ff origin/feature_lyl_calendarList_0620  # 解决冲突如果有

为增加changeid增加如下一步
git commit --amend

git push origin HEAD:refs/for/develop

# 上线时,专人把develop merge到master,参照步骤

git checkout master

git pull

git merge --no-ff origin/develop


为增加changeid增加如下一步
git commit --amend

git push origin HEAD:refs/for/master



git fetch --prune

一次性把主线上的改动合并到功能分支用这个
git merge --no-ff origin/develop

/////////////
利用 reflog 撤销变基
git reflog
git reset --hard HEAD@{3}

master与develop一般会是两个不同的分支,只能合并,不用rebase


-----------------------------------Linux(windows)下以https(用户名密码)方式同步代码
git clone https://github.com/xxx/shell.git
...
git commit
git push -u origin master
回复

使用道具 举报

1228

主题

1997

帖子

7582

积分

认证用户组

Rank: 5Rank: 5

积分
7582
17#
发表于 2022-10-27 10:03:04 | 只看该作者
本帖最后由 Qter 于 2022-10-27 10:09 编辑

功能分支合并到主分支并删除功能分支

git checkout master

git pull # merge 之前 pull 一下,让 master 更新到和远程仓库同步


git merge feature1




git push


git branch -d feature1


git push origin -d feature1# 用 -d 参数把远程仓库的 branch 也删了






推送到远程分支
git checkout feature1git push origin feature1

回复 支持 反对

使用道具 举报

1228

主题

1997

帖子

7582

积分

认证用户组

Rank: 5Rank: 5

积分
7582
16#
发表于 2022-10-26 19:17:47 | 只看该作者
如果觉得一遍遍地输入密码很烦,可以按照这个页面 提供的方案来把密码保存起来。
另外还有一个更简单但安全性低一些的方案。执行这行代码:
git config credential.helper store
在这之后你只需要再输入一次密码, Git 就会把你的密码保存下来,这之后就再也不用输入了。说它「安全性低」,是因为这条指令会让 Git 把你的密码以明文形式保存在你的电脑上。具体这两种保存密码的方案选择哪个,看你自己了。
回复 支持 反对

使用道具 举报

1228

主题

1997

帖子

7582

积分

认证用户组

Rank: 5Rank: 5

积分
7582
15#
发表于 2021-12-1 14:00:11 | 只看该作者
重命名git tag
1. git tag newtag oldtag

2. git tag -d oldtag


3. git push origin :refs/tags/oldtag


4. git push --tags


回复 支持 反对

使用道具 举报

1228

主题

1997

帖子

7582

积分

认证用户组

Rank: 5Rank: 5

积分
7582
14#
发表于 2021-3-8 15:33:38 | 只看该作者
[backcolor=var(--color-box-header-blue-bg)]Quick setup — if you’ve done this kind of thing before[color=var(--color-btn-text)][backcolor=var(--color-btn-bg)] Set up in Desktop
or
HTTPSSSH




Get started by [color=var(--color-text-link)]creating a new file or [color=var(--color-text-link)]uploading an existing file. We recommend every repository include a [color=var(--color-text-link)]README, [color=var(--color-text-link)]LICENSE, and [color=var(--color-text-link)].gitignore.

…or create a new repository on the command line[backcolor=var(--color-bg-tertiary)]
echo "# MyNotebook" >> README.mdgit initgit add README.mdgit commit -m "first commit"git branch -M maingit remote add origin git@github.com:hechengjin/MyNotebook.gitgit push -u origin main

…or push an existing repository from the command line[backcolor=var(--color-bg-tertiary)]
git remote add origin git@github.com:hechengjin/MyNotebook.gitgit branch -M maingit push -u origin main

…or import code from another repository
You can initialize this repository with code from a Subversion, Mercurial, or TFS project.
[color=var(--color-btn-text)][backcolor=var(--color-btn-bg)]Import code




回复 支持 反对

使用道具 举报

697

主题

1142

帖子

4086

积分

认证用户组

Rank: 5Rank: 5

积分
4086
13#
 楼主| 发表于 2017-9-20 16:40:25 | 只看该作者
本帖最后由 java 于 2017-11-30 16:10 编辑

Linux下以https(用户名密码)方式同步代码
git clone https://github.com/xxx/shell.git
...
git commit
git push -u origin master


http://www.111cn.net/sys/linux/79505.htm



git reset --hard ecd265ac831681c035479d69948313ebf3a4c736  //不保留改动过的内容


git reset 215b364ca8b6f8134230dfba38446594fbb31555   //保存改动过的内容

回复 支持 反对

使用道具 举报

697

主题

1142

帖子

4086

积分

认证用户组

Rank: 5Rank: 5

积分
4086
12#
 楼主| 发表于 2017-8-17 18:25:27 | 只看该作者
本帖最后由 java 于 2018-9-29 14:31 编辑

查看远程分支
git branch -a

查看本地分支
git branch

创建分支
git branch test

切换分支到test
git checkout test


删除本地分支
git branch -d test

删除远程分支
命令行 : $ git push origin --delete <BranchName>


查看clone的源码路径
git remote -v




git remote -av

git branch -av
回复 支持 反对

使用道具 举报

697

主题

1142

帖子

4086

积分

认证用户组

Rank: 5Rank: 5

积分
4086
11#
 楼主| 发表于 2017-8-17 18:17:26 | 只看该作者
本帖最后由 java 于 2017-11-23 16:16 编辑

missing change-id


git commit -a --amend -s

查看某条日志的具体修改内容
git show cdb90e69217a7ac65bc6e52e2622066a05aa10cf

用git show <commit-hashId> 便可以显示某次提交的修改内容
同样 git show <commit-hashId> filename 可以显示某次提交的某个内容的修改信息。

查看某一文件的历史性改动

git log --pretty=oneline 文件名

查看某一目录的修改日志
git log --pretty=format:'%h was %an, %ar, message: %s' srcdir

修改git log默认的时间显示方式
git config log.date iso  临时git config log.date iso  --global


查看配置信息
npm config ls -l

设置代理
npm config set proxy http://proxyhostname:proxyport
npm config set https-proxy http://proxyhostname:proxyportnpm的代理设置

git config --global http.proxy "localhost:1080"
git config --global https.proxy http://proxy.com:1234
git config --global https.proxy https://127.0.0.1:8580

移除代理
npm config rm proxy
npm config rm https-proxy
>npm config set proxy null
>npm config set https-proxy null

npm config set proxy false
npm cache clean

--global~/.gitconfig
--system
$(prefix)/etc/gitconfig
--local
.git/config


//config
git config --global -l
git config --global --list

git config --global --unset http.sslverify


git config --global https.proxy http://127.0.0.1:1080
git config --global https.proxy https://127.0.0.1:1080
git config --global --unset http.proxy
git config --global --unset https.proxy
npm config delete proxy
git config --global http.proxy 'socks5://127.0.0.1:1080'
git config --global https.proxy 'socks5://127.0.0.1:1080'

如果清理完了,还是连接原来的端口,则是下面shell脚本设置了环境变量
  1. export http_proxy=http://127.0.0.1:8787
  2. export https_proxy=$http_proxy
  3. git config --global http.proxy $http_proxy
  4. git config --global https.proxy $https_proxy
复制代码

/etc/profile
~/.bashrc       lantern会写到其它文件,直接在这重新定义  export http_proxy=‘’
~/.profile

日志统计
git shortlog -sn  按照提交的commit数量进行统计
git shortlog -sn --since="7 weeks" --until="1 weeks"  对某一个时间段进行统计










查看指定用户的git提交记录git log --author=username
username为指定的用户



回复 支持 反对

使用道具 举报

55

主题

78

帖子

289

积分

认证用户组

Rank: 5Rank: 5

积分
289
10#
发表于 2017-3-27 17:31:52 | 只看该作者
git clone ssh://xxx@192.168.21.51:3333/proj2
回复 支持 反对

使用道具 举报

55

主题

78

帖子

289

积分

认证用户组

Rank: 5Rank: 5

积分
289
9#
发表于 2017-3-25 14:14:53 | 只看该作者

使用git注意点: 提交前一定要更新到最新代码


万一忘记更新,又进行了提交  则要先进行回退 然后 重新提交
回退过程
1.git log
...
commit abedfef27cc7061e555bdbb9772abc74549a2369
....


commit c685c972f037205c7a235c8582e84c998151766e


...
commit 09ef555dcf45a7cda19d9433626f46dfbaf59fc0



如回退到第三个绿色的部分
git reset 09ef555dcf45a7cda19d9433626f46dfbaf59fc0


git status


git add .

git pull --rebase



另外如果中间有需要的加下
git stash
git stash pop


回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|firemail ( 粤ICP备15085507号-1 )

GMT+8, 2024-5-5 21:37 , Processed in 0.074619 second(s), 22 queries .

Powered by Discuz! X3

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表