Skip to content

git 环境

windows git 上有分支名称,一下加入可让 linux 也支持分支名称

vim ~/.bashrc

  • 方案1:
function git-branch-name {
  git symbolic-ref HEAD 2>/dev/null | cut -d"/" -f 3
}
function git-branch-prompt {
  local branch=`git-branch-name`
  if [ $branch ]; then printf " [%s] " $branch; fi
}

if [ "$color_prompt" = yes ]; then
    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]$(git-branch-prompt)\$ '
else
    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w$(git-branch-prompt)\$ '
fi
  • 方案2:

git env

显示分支名称

parse_git_branch() {
    git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}

PS1="\[\033[36m\]\u@\h\[\033[m\]:\[\033[32m\]\w\[\033[m\]\$(parse_git_branch)\$ "

配色

配置文件

vim /home/[username]/.gitconfig

[color]
    status = auto
    diff = auto
    ui = true
    interactive = auto
    branch = auto

修改后立马生效

命令设置

git config --global color.status auto
git config --global color.diff auto
git config --global color.branch auto
git config --global color.ui true
git config --global color.interactive auto

1 远程操作

1.1 获取远程仓库地址信息

git remote -v

1.2 本地新建项目情况

本地建立仓库,远程后建立,拉取时会失败,使用以下命令强制拉取

git pull <远程仓库地址> -allow-unrelated-histories 

1.3 更新当前分支远程仓库分支地址

git branch --set-upstream-to=origin/<远程分支名称>

1.4 删除远程仓库

git push origin --delete origin/<远程分支名称>

1.5 远程分支回滚

  1. 要回滚的版本号
git reset --hard <commit> 
  1. 提交回退
// 方式1
git push origin HEAD --force   
// 方式2
git push --force

1.6 回滚作为一个更改项,避免已提交到远程分支后同步造成冲突

git revert <commit>     // 回退作为一个动作保留
git push                // 这样作为一个同步提交不会造成服务器的冲突了

1.7 更新远程仓库地址

git remote set-url origin 【url】

或者

git remote remove origin
git remote add origin【url】

1.8 克隆单个分支

git clone -b <branch> <remote_repo>

2 本地操作

2.1 拉取其他仓库所有分支作自己分支

fetch <仓库路径>

2.2 当前分支获取其他分支某个提交的改动

git cherry-pick <版本号> # 也可以是分支名称

3 打标签

3.1 显示标签

$ git tag

3.2 创建标签

$ git tag -a v1.0 -m "xxx"

3.3 显示标签所有前缀标签

```\ $ git tag -l "v1.0*"

#### 3.4 显示对应标签所有信息
$ git show v1.0
#### 3.5 删除标签
$ git tag -d v1.0
## git submodule

### 添加子模块

```sh
git submodule add <submodule> <path>

拉取整个项目

git submodule update --init --recursive

删除子模块

git submodule deinit <path>
git rm <path>

如果已经update 了,还要删除 .git里面module的对应内容

更换子模块

先删除再添加

  1. git submodule deinit -f {submodule_name}
  2. 删除 .gitsubmodule 中对应条目
  3. rm -rf .git/modules/{submodule_name}
  4. 如果 .git/config 中有对应信息需删除
  5. 添加新的子模块

更新子模块路径

git 1.8.5 之后新增, 建议先删除再新增子模块

git mv {oldPath} {newPath}
# 如果要更换submodule 名称与 path 一致
mv .git/modules/{oldPath} .git/modules/{newPath}

Comments