0%

Git使用详解

1. 常用操作

设置默认编辑器为vim

1
git config --global core.editor "vim"

终端显示中文乱码

1
git config --global core.quotepath false

Convert remote’s URL from https to ssh:

1
2
3
4
# To check if remote's URL is ssh or https
git remote -v
# To switch from https to ssh
git remote set-url origin git@github.com:USERNAME/REPOSITORY.git

Untrack files and don’t change working directory:

1
2
3
git rm --cached [filename]# untrack specified file
git reset HEAD file
git reset HEAD . # untrack all files

Reset

1
2
3
4
5
6
# reset staging area and reset working directory to match last commit
git reset --hard
# move the current brach tip backward to <commit>, reset the staging area to match, but leave the working directory alone
git reset <commit>
# same as precious, but resets both the staging area and working directory
git reset --hard <commit>

Clean files:

1
2
3
4
# Show which files would be removed from working directory
git clean -n
# Execute the clean
git clean -f

Append changes to last commit:

1
git commit --amend

Display commits concerned with the specific file:

1
git log -- <file>

Diff

1
2
3
4
5
6
# show difference between working directory and last commit
git diff
# show difference between changes and last commit
git diff --cached
git diff --name-only
git diff --name-status

Stash

1
2
3
4
5
6
git stash
git stash -- file_01 file_02
git stash -p
git stash list
git stash show -p stash@{0}
git stash drop stash@{0}

Patch

1
2
3
4
git format-patch <commit-id-head>
git am --abort
git am *.patch
git am *.patch --reject

Reflog

1
git reflog

Tag

1
2
3
4
git tag -a v0.1 -m ""
git tag -a v0.2 9fceb028
git push origin tag_name
git push origin --tags

CherryPick

1
git cherry-pick A^...B

Checkout

1
git checkout branch_name/commit_id/tag_name -- file_name

2. 常见问题

file mode changes

That looks like unix file permissions modes to me (755=rwxr-xr-x, 644=rw-r--r--) - the old mode included the +x (executable) flag, the new mode doesn’t.

1
2
old mode 100755  
new mode 100644

解决思路

Setting core.filemode to false in order to get rid of the issue:

1
git config core.filemode false

Reference: https://stackoverflow.com/questions/1257592/how-do-i-remove-files-saying-old-mode-100755-new-mode-100644-from-unstaged-cha

部分仓库下载缓慢/失败问题

方法一

对于不包含submodule的仓库,出现这种问题后,从网页版GitHub中下载源码即可。

方法二

对于包含submodule的仓库,一般是主仓库可以下载,但是其submodule下载失败。此时,可以在clone主仓库后,分别clone每个子仓库,再将其放置到父仓库对应的目录下,然后通过git reset --hard commit_id将子仓库切换到对应版本。

1
2
3
4
5
6
git clone xxx.git repo-a --recursive # failed
git clone xxx.git repo-a # success
cd repo-a/third_parth
git clone xxxx.git repo-b --recursive # success or fail
cd repo-b
git reset --hard commit_id

方法三

除了上述两种方法外,可以借助我的工作台 - Gitee.com解决该问题。操作时首先将需要clone的仓库从GitHub中fork到Gitee中,然后从Gitee clone即可。对于包含submodule的仓库,可以将其submodule均fork到Gitee,并修改.submodule中对应仓库的路径为Gitee下的路径(注意此处路径结尾不包含.git字段).

------ 本文结束 ------