Git 常用命令

本地新建项目并关联到远程仓库

git init
git remote add origin repository-url
git push --set-upstream origin master

添加远程仓库地址

git remote set-url origin new-repository-url

恢复单个文件版本

git checkout <hash> <filename>

导出代码

git archive -o ../updated.zip HEAD $(git diff --name-only HEAD^)

移除版本控制

1
2
git rm -r -n --cached docs/api	# -n 预览命令,并不真正删除
git rm -r --cached docs/api

将某个commit移动到其它branch

git cherry-pick <commit id>

比较差异

  1. 比较工作区与暂存区

    git diff [path]

  2. 比较暂存区与最新本地版本库

    git diff --cached [path]

    git diff --staged [path]

  3. 比较commit

    git diff [commit-id] [path]

    git diff [commit-id] [commit-id]

  4. 比较branch

    git diff [branch] [path]

    git diff [branch] [branch]

中文文件名及路径乱码

git config --global core.quotepath false

查看修改记录

git log [options] [path]

-p 显示具体差异

–follow 查看文件改名之前的修改记录

–merges 仅查看merge记录

–author=xx 查看指定author的修改记录

同步 Github fork 出来的分支

git remote add upstream fork-source-repo-url
git fetch upstream
git merge upstream/master
git push

查看储藏区(stash)的差异

git stash show -p stash@\{0\} --exit-code

####设置显示名称及邮箱

git config --local user.name "Wayde"

git config --local user.email "email_address"

修改commit的author信息

git commit --amend --reset-author

参考资料:
git-archive - Create an archive of files from a named tree