Git-常用命令整理


远程分支强制覆盖本地分支(超级常用)

置顶是因为我自己用了无数遍呀!!

1
2
3
git fetch --all
git reset --hard origin/your branch name
git pull

快捷提交

1
git commit -a -m 'this is my commit'

等价于

1
2
git add .
git commit -m 'your commit'

命令简写(mac环境,windows没试)

命令行输入:

1
alias

会列出所有git相关的缩写,当然也可以自己配。
alias
常用的也就几个:

1
2
3
4
5
6
7
8
9
10
切换指定分支:
gco branch_name
切换到master分支:
gcm
查看分支:
gb
查看修改:
gd
拉代码:
gl

新建没有parent的新分支

1
git checkout --orphan branch-name

Git 代码量

1
git log --format='%aN' | sort -u | while read name; do echo -en "$name\t"; git log --author="$name" --pretty=tformat: --numstat | awk '{ add += $1; subs += $2; loc += $1 - $2 } END { printf "added lines: %s, removed lines: %s, total lines: %s\n", add, subs, loc }' -; done

Git 命令提示:perl: warning: Setting locale failed.

1
2
3
4
5
6
7
8
9
vim  ~/.zshrc

# Setting for the new UTF-8 terminal support in Lion
LC_CTYPE=en_US.UTF-8
LC_ALL=en_US.UTF-8

source ~/.zshrc

重启iTerm

merge某个或几个commit

有些特殊情况不能直接merge(一个分支上同时有你和别人的commit),但是你的commit需要上线了。

  • 查找要merge的commitId
    1
    2
    gco old_branch
    git log

    commit 1534c530522982a6ff050ac420b300ae4364474e
    Author: lidoudou <lidoudou@csdn.com>
    Date: Sat Apr 27 17:15:14 2019 +0800
    update kafka setting

  • 切换到目标分支,pick目标commit
    1
    2
    gco new_branch
    git cherr-pick 1534c530522 // commitId
  • 正常的话现在可以看到new_branch上有了commitId不同,但是标识名相同的记录。
  • 当然也会出现意外——代码冲突了,按一般的冲突一样fix就OK了。

  目录