首页 > 技术文章 > git常用操作记录

hanshuai 2020-04-07 10:44 原文

git常用操作记录,持续更新。。。

# clone仓库中指定tag:

$ git clone -b v1.0.2 git@172.16.79.20:frontend/project-a.git

# 说明
# git clone -b <tags> <repository>

 # 同步origin仓库的对remote仓库分支追踪关系

# remote仓库删除分支,origin仓库还保留remote仓库删除前的分支追踪关系
$ git remote prune origin

# 查看origin仓库和remote仓库 追踪关系

$ git remote show origin
* remote origin
  Fetch URL: git@172.16.79.20:front-end/elderly-care-web.git
  Push  URL: git@172.16.79.20:front-end/elderly-care-web.git
  HEAD branch: master
  Remote branches:
    dev    tracked
    master tracked
  Local branches configured for 'git pull':
    dev    merges with remote dev
    master merges with remote master
  Local refs configured for 'git push':
    dev    pushes to dev    (up to date)
    master pushes to master (up to date)

 

# 删除origin仓库分支

# 删除origin仓库分支 dev,-D = --delete --force (表示强制删除)
$ git branch -D dev
Deleted branch dev(was 0f59de7).

# 删除remote仓库分支

# 删除remote仓库分支 dev

# 方法1 
$ git push origin -d dev
 - [deleted]         dev # 方法2(说明:推一个空的分支覆盖远端分支,相当于删除远端分支) $ git push origin :dev
 - [deleted]         dev

# git分支设置描述信息

$ git config branch.bugfix_1.description '设置用户删除报错的bug分支'

# 修改git分支的描述信息

$ git config branch.bugfix_1.description '修改用户删除报错的bug分支'
warning: branch.haiyan-outer.description has multiple values
error: cannot overwrite multiple values with a single value
       Use a regexp, --add or --replace-all to change branch.haiyan-outer.description.

# 提示描述信息已经设置过了,我们可以再次覆盖修改掉
$ git config --replace-all branch.bugfix_1.description '修改用户删除报错的bug分支'

 

# 查看git分支的描述信息

$ git config branch.bugfix_1.description
修改用户删除报错的bug分支

 # 合并commit提交记录

##1 查看当前分支的提交记录(发现最近的3次commit改的是相同的模块)
$ git log
commit 2e91644fa69cb8e9a6af7e88062341b4042cac67 (HEAD -> bugfix_track)
Author: hanshuai <hanshuai@szhq.com>
Date:   Tue Apr 21 17:29:12 2020 +0800

    fix:修改向前向后推月份年份后日期出现的bug

commit 57e7897129d12af3ae934a5032c70d2a3b84d9a0
Author: hanshuai <hanshuai@szhq.com>
Date:   Tue Apr 21 15:37:04 2020 +0800

    fix:修改在无轨迹数据的当月选择前一天后一天无法去掉loading的bug

commit 0da29036e72021a7e05d2de6778a10af414ab324
Author: hanshuai <hanshuai@szhq.com>
Date:   Tue Apr 21 15:14:53 2020 +0800

    fix:修改日期选择器外边跟里边日期不同步的bug

commit 07dd886065a888392c91acdbf59a317aaef66be6 (origin/dev, dev)
Merge: 1c1113d 4fec4e7
Author: hanshuai <hanshuai@szhq.com>
Date:   Mon Apr 20 11:30:04 2020 +0800

##2 合并最近的3次commit为一次commit(~3表示最新的3条commit)
方式一 $ git rebase
-i HEAD~3

方式二

$ git rebase -i 07dd886 [2e91644] (注意:有范围的时候是前开后闭"(]",意思是开始位置不包括07dd886这个分支,结束位置的如果不填则默认为最近提交的一次commit)

##2.1 出现编辑合并的commit信息 reword 0da2903 fix:修改日期选择器外边跟里边日期不同步的bug fixup 57e7897 fix:修改在无轨迹数据的当月选择前一天后一天无法去掉loading的bug fixup 2e91644 fix:修改向前向后推月份年份后日期出现的bug # Rebase 79db0bd..9ac1179 onto 79db0bd (3 commands) # # Commands: # p, pick = use commit # r, reword = use commit, but edit the commit message # e, edit = use commit, but stop for amending # s, squash = use commit, but meld into previous commit # f, fixup = like "squash", but discard this commit's log message # x, exec = run command (the rest of the line) using shell # d, drop = remove commit # # These lines can be re-ordered; they are executed from top to bottom. # # If you remove a line here THAT COMMIT WILL BE LOST. # # However, if you remove everything, the rebase will be aborted. # # Note that empty commits are commented out ##2.2 对reword的那次commit进行确认 fix:修改日期选择器和历史轨迹不同步的bug # Please enter the commit message for your changes. Lines starting # with '#' will be ignored, and an empty message aborts the commit. # # Date: Tue Apr 21 15:14:53 2020 +0800 # # interactive rebase in progress; onto 07dd886 # Last command done (1 command done): # reword 0da2903 fix:修改轨迹追踪中日期选择器和外边交互bug # Next commands to do (2 remaining commands): # fixup 57e7897 fix:修改在无轨迹数据的当月选择前一天后一天无法去掉loading的bug # fixup 2e91644 fix:修改向前向后推月份年份后日期出现的bug # You are currently editing a commit while rebasing branch 'bugfix_track' on '07dd886'. # # Changes to be committed: # modified: src/components/date-picker/date-picker.vue # modified: src/modules/main/location/location/history/locationHistory.vue # modified: src/modules/main/location/location/history/track.vue # ##2.3 关闭commit默认编辑器后 [detached HEAD 30139db] fix:修改日期选择器和历史轨迹不同步的bug Date: Tue Apr 21 15:14:53 2020 +0800 3 files changed, 54 insertions(+), 10 deletions(-) Successfully rebased and updated refs/heads/bugfix_track. ##3 查看合并后的提交记录(之前的3次commit合并为一次 c68dfe8 ) commit c68dfe8d9997600420bacced6b4615e842e7c91c (HEAD -> bugfix_track) Author: hanshuai <hanshuai@szhq.com> Date: Tue Apr 21 15:14:53 2020 +0800 fix:修改日期选择器和历史轨迹不同步的bug commit 07dd886065a888392c91acdbf59a317aaef66be6 (origin/dev, dev) Merge: 1c1113d 4fec4e7 Author: hanshuai <hanshuai@szhq.com> Date: Mon Apr 20 11:30:04 2020 +0800 Merge branch 'dev' of 172.16.79.20:front-end/new-electric-bicycle-web into dev commit 1c1113d764e0e9dba59e245d2b431ac828fe991f Author: hanshuai <hanshuai@szhq.com> Date: Mon Apr 20 11:29:50 2020 +0800 refactor:重构综合数据统计响应式样式

##4 把合并结果提交到远程仓库

git push -f (注:必须加参数-f,不然你本地合并的commit会叠加到远程最后一次commit之后)  

 合并中可能出现的问题

// 我们rebase操作完后,可能会出现 Successfully rebased and updated detached HEAD.
hs@DESKTOP-L2NPMSM MINGW64 /d/project/js/new-electric-bicycle-h5/code/new-electric-bicycle-h5 ((e84468e...))

$ git push -f
fatal: You are not currently on a branch.
To push the history leading to the current (detached HEAD)
state now, use

    git push origin HEAD:<name-of-remote-branch>


// 解决
// 将当前游离态的HEAD强行推向远程master仓库
git push -f origin HEAD:master

// 远程覆盖本地
hs@DESKTOP-L2NPMSM MINGW64 /d/project/js/new-electric-bicycle-h5/code/new-electric-bicycle-h5 ((e84468e...))
$ git fetch --all &&git reset --hard origin/master && git pull
Fetching origin
HEAD is now at e84468e test:降低swiper的版本
You are not currently on a branch.
Please specify which branch you want to merge with.
See git-pull(1) for details.

    git pull <remote> <branch>

//  提示当前我们不处于任何一个分支,先切换会master
git checkout master

// 再一次远程覆盖本地
$ git fetch --all &&git reset --hard origin/master && git pull
Fetching origin
HEAD is now at e84468e test:降低swiper的版本
Already up to date.

// 查看
$ git branch -v
* master e84468e test:降低swiper的版本

# 暂存本地修改(你在开发一个新功能,未开发完的时候,你又不想commit,git远端仓库别人后提交,可以将本地修改暂存起来,将远端变化代码pull下来)

# 暂存修改 git stash save 'stash message'

git stash -u 或者 git stash save --include-untracked # 存储git未追踪的文件(未添加到暂存区的文件)

# 查看stash列表 

$ git stash list
stash@{0}: On master: 新增a.md文件

# 查看stash修改(untracked file显示为空)

git stash show (默认第一个stash)
git stash show stash@{1} (第二个stash)

# 查看stash修改详情(untracked file显示为空)

git stash show -p (默认第一个stash)
git stash show stash@{1} -p (第二个stash)

# 应用一个stash修改(不会从stash list中删除)

git stash apply (默认第一个stash)
git stash apply stash@{1} (第二个stash)

# 恢复一个stash修改(会从stash list中删除,类似于数组的pop操作)

git stash pop (默认第一个stash)
git stash pop stash@{1} (第二个stash)

# 删除一个stash

git stash drop (默认第一个stash)
git stash drop stash@{1} (第二个stash)

# 删除所有stash修改

git stash clear

 

推荐阅读