常用Git命令使用整理

git branch
#查看远程分支
git branch -vv

#repo 如果本地分支不是某个分支则打印出路径和当前分支:
repo forall -p -c 'CUR_BRANCH=`git branch |grep "*"` && IS_BRANCH=`git branch |grep "*" |grep "^* Fox_ZOOM$"` || (pwd && echo "$CUR_BRANCH")'
#如果远程分支不是某个分支则打印出路径和当前远程分支
repo forall -c 'REMOTE_BRANCH=`git branch -vv |grep "*"| cut -d "/" -f2|cut -d "]" -f1 |cut -d ":" -f1` && IS_BRANCH=`echo "$REMOTE_BRANCH" |grep "^Fox_ZOOM$"` || (pwd && echo $REMOTE_BRANCH)' 


function _git_is_branch {
    CHOOSE_BRANCH=`git branch |grep "\*"`
    if [[ "$CHOOSE_BRANCH" != "* $1" ]];then    
        echo "$(pwd) branch:$CHOOSE_BRANCH"
    fi
}

function _repo_is_branch {
    CMD="CUR_BRANCH=\`git branch |grep \"*\"\` && IS_BRANCH=\`git branch |grep \"*\" |grep \"^* $1\$\"\` || (pwd && echo \"\$CUR_BRANCH\")"
    echo "$CMD"
    repo forall -p -c "$CMD"
}
function _repo_is_remote_branch {
    CMD="REMOTE_BRANCH=\`git branch -vv |grep \"*\"| cut -d \"/\" -f2|cut -d \"]\" -f1 |cut -d \":\" -f1\` && IS_BRANCH=\`echo \"\$REMOTE_BRANCH\" |grep \"^$1$\"\` || (pwd && echo \"\$REMOTE_BRANCH\")"

    echo "$CMD"
    repo forall -c "$CMD"
}

git remote
#查看remote project path
git remote -v


下载代码相关:

git clone
git clone ${code-url}
git clone -b ${branch} ${code-url}
git checkout
#回退add但未commit的文件
git checkout . 
#切换分支
git checkout ${TARGET_BRANCH} 
#切换到某个tag
git checkout ${TARGET_TAG}


repo start
#remote分支被重新指定
repo start ${Branch_name} --all

###上传代码相关:

git commit

如果不小心git commit --amend,想要撤回提交,可以先git reflog查询要撤回到哪个提交,然后使用git reset $commit_id进行撤回。

git push
git push --no-thin
git push `git remote` tag
git push `git remote` branch_name
#等同于repo upload 
git push HZGerrit HEAD:refs/for/FOX_APEX
git clean
#列出将会被清除的文件
git clean -n 
#删除未add的文件
git clean -f
#删除指定的未add的文件
git clean -f <path>
#删除未add的目录和文件
git clean -df
#删除包含.gitignore中未add的文件
git clean -xf
#git reset --hard和git clean -df 结合使用使工作目录完全回退到最近一次commit的时候
git merge
#将a分支合入到b分支
git checkout b
git merge a
git push `git remote` b

# git merge 默认是支持Fast-forward模式,即如果分支b上没有提交过其他,直接将a分支上提交的内容全部都合入b,将a的head指定为b的head提交.
#如果你本地支持git push,这样一次性合入很精简,也挺好的,就是看不出来你是从b分支merge来的。
#如果你使用repo upload来提交,这样的话,b分支上提交很多就会导致gerrit上又要审核很多条commit,非常反锁。我们可以关闭Fast-forward模式。将所有提交合并到一个merge提交
git merge b --no-ff

#确保b的提交均已合入了a
 repo forall -c 'pwd && git log --oneline --left-right a..b
git rebase
#基于某个提交开始处理后续的提交。可以单独合入一个提交,也可以合并某个提交到它的前一个提交
git rebase -i $FROM_COMMIT_ID

###Tag相关:

git tag
#给当前commit打标签
git tag -a `cat $tag_name` -m `cat $tag_book_mark`
#显示某tag详情
git show tag_name
#提交当某tag到远程仓库
git push `git remote` tag_name
#推送全部tag
git push --tags
#列出全部tag
git tag -l
#搜索某tag
git tag -l ‘v0.1.*'

#删除本地某tag
git tag -d tag_name

#删除远程仓库中的某tag
git push `git remote` --delete tag tag_name
#远程仓库更新本地tag
git fetch --tags

log相关:

git log
#a分支与b分支对比
git log --left-right a...b
git log --after="2020-12-11 05:00:00" --graph --pretty=format:"%h %s (%an, %cd)"
repo forall -p -c 'git log --after="2020-12-11 05:00:00" --graph --pretty=format:"%h %s (%an, %cd)"' 
#仅打印出有log的路径
repo forall -c 'CUR_LOG=`git log --after="2022-04-20 17:00" --graph --pretty=format:"%h %s (%an, %cd)"` &&  (test -z "$CUR_LOG" || (pwd && echo "$CUR_LOG"))' 
function _repo_log_after {
    CMD="CUR_LOG=\`git log --after=\"$1\" --graph --pretty=format:\"%h %s (%an, %cd)\"\`  &&  (test -z \"\$CUR_LOG\" || (pwd && echo \"\$CUR_LOG\"))"
    echo "$CMD"
    repo forall -c "$CMD"
}
git blame
按行查看文件的提交记录

git blame -L n1,n2 [file]

常用问题

1.the target branch is a symbolic ref

Could not perform action: Failed to submit 1 change due to the following problems:
Change 246494: the target branch is a symbolic ref
最新gerrit上,合入的目标分支是个链接分支会报错,找到对应的实际分支名称,合入这个分支可以
比如branch1引用了master分支, gerrit上要合入master

2. UnicodeDecodeError: ‘ascii’ codec can’t decode byte 0xe2 in position 128: ordinal not in range(128)

采用 Python报错“UnicodeDecodeError: ‘ascii’ codec can’t decode byte 0xe9 in position 0: ordinal not in range(128)”的解决办法 中的第2种方法

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值