Git Product home page Git Product logo

noscripter.github.io's Introduction

Keep it simple and cool.

noscripter.github.io's People

Contributors

noscripter avatar

Watchers

 avatar

Forkers

rettpop

noscripter.github.io's Issues

[Git Tips] fetch all branches of remote after clone with specific depth

Easy way:

git remote set-branches origin '*'

Verbose way:

1. edit git config

before:

[core]
	repositoryformatversion = 0
	filemode = true
	bare = false
	logallrefupdates = true
	ignorecase = true
	precomposeunicode = true
[remote "origin"]
	url = https://github.com/ReactiveX/rxjs
	fetch = +refs/heads/master:refs/remotes/origin/master
[branch "master"]
	remote = origin
	merge = refs/heads/master

after:

[core]
	repositoryformatversion = 0
	filemode = true
	bare = false
	logallrefupdates = true
	ignorecase = true
	precomposeunicode = true
[remote "origin"]
	url = https://github.com/ReactiveX/rxjs
	fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
	remote = origin
	merge = refs/heads/master
2. run git fetch to get remote branches
git fetch -v

Git技巧

Git技巧

1. 如何回退到某一次提交?

本地回退到某次提交:

git checkout xxx-commit-hash

远程分支回退到某次提交:

git reset --hard xxx-commit-hash

跟着上面的命令,先将工作目录回退到某次提交,然后执行下面的命令:

git push -f origin xxx分支

2. 修改上次提交的提交信息

git commit --amend -m 'xxxx'
git push remote branch --force # 或者 git push remote branch -f

3. 删除远程分支

git push origin :<branchName>

4. 比较两个不同分支上的相同文件的不同

git diff branch1 branch2 xxx_file

5. 查看两个分支有哪些文件做了修改

git diff --name-status branch1 branch2

或者:

git diff --stat --color branch1 branch2

6. 查看当前库的所有远端版本

git branch -ar
git ls-remote
git remote show origin

有时候本地的远程信息没有同步,需要执行下面的命令同步:

git remote update

7. 查看配置信息

git config --list

或者:

git config -l

查看所有配置信息:

git config --get-all user.name

或者:

git config user.name

查看某个值的内容:

git config --global -l

查看全局配置

8. 配置文件位置(按优先级从低到高):

/etc/gitconfig
~/.gitconfig

9. 配置当前库的git信息:

git config --local user.name 'xxxx'
git config --local user.email '[email protected]'

也可以通过修改.git/config下的信息来修改当前仓库的git信息。

10. 查看当前库的git信息:

git config --local -l

11. 修改历史提交日志

#!/bin/sh

git filter-branch --env-filter '
OLD_EMAIL="[email protected]"
CORRECT_NAME="Your Correct Name"
CORRECT_EMAIL="[email protected]"
if [ "$GIT_COMMITTER_EMAIL" = "$OLD_EMAIL" ]
then
    export GIT_COMMITTER_NAME="$CORRECT_NAME"
    export GIT_COMMITTER_EMAIL="$CORRECT_EMAIL"
fi
if [ "$GIT_AUTHOR_EMAIL" = "$OLD_EMAIL" ]
then
    export GIT_AUTHOR_NAME="$CORRECT_NAME"
    export GIT_AUTHOR_EMAIL="$CORRECT_EMAIL"
fi
' --tag-name-filter cat -- --branches --tags

12. 撤销本地已经commit但是还没有push的修改

git reset --hard HEAD~1 #撤销最近一次commit,即从HEAD恢复到上一次commit

13. 查找具体的修改内容是什么时候commit的

git log -G"正则表达式"

例如:git log -G"preswitch"

14. 修改提交历史记录中的特定内容

git filter-branch --commit-filter '
        if [ "$GIT_AUTHOR_NAME" = "noscripter" ];
        then
                GIT_AUTHOR_NAME="anonymous";
                GIT_AUTHOR_EMAIL="[email protected]";
                git commit-tree "$@";
        else
                git commit-tree "$@";
        fi' HEAD

15. 删除所有历史提交记录,只保存当前内容。

rm -rf .git
git init
git add .
git commit -m "Initial commit"
git remote add origin
git push -u --force origin master

16. 为github添加了sshkey,但是仍然提示需要输入用户名和密码。

修改.git/config中的origin信息,将https协议改为[email protected]:xxx/project-name

或者使用git remote set-url origin [email protected]:username/repo.git

17. 如何测试github上的sshkey已经添加成功?

18. 如果使用ssh push时提示Permission denied (publickey).应该如何处理?

错误信息为:

Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

解决方法:

eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_rsa
ssh-add -l

注意,上面第二步ssh-add加入的是密钥,而不是公钥。

如果ssh-add -l运行后显示The agent has no identities.说明没有把公钥加入到ssh客户端中。

参考链接:

19. 查看指定目录的修改记录

git diff --relative=[path]

20. 查看git修改的概况

git diff --stat

21. 比较不同分支上的同一个文件

git diff mybranch master -- myfile.cs

22. 查看当前git库文件所在的本地目录

git rev-parse --show-toplevel

其他

23. 怎样下载一个github仓库中的特定目录?

例如:

svn checkout https://github.com/flazz/vim-colorschemes/trunk/colors/

其中trunk对应github的master分支。

24. 修改某次commit的author信息

git commit --amend --author="Author Name <[email protected]>"

25. 不能添加文件夹(new folder not show in git status)

git status --ignored
git add --force folder

26. git查看要提交的内容

git diff --stat --cached [remote/branch]
git diff [remote repo/branch]
git diff --numstat [remote repo/branch]
git push --dry-run

27. git撤销即将提交的修改

git reset --hard [remote branch]

28. 设置git ignore核心文件

git config --global core.excludesfile '~/.gitignore'

28. 快速打开当前目录的gitlab或者github链接

首先在~/.gitconfig中配置如下alias

[alias]
  url =! bash -c 'git config --get remote.origin.url | sed -E "s/.+:\\(.+\\)\\.git$/https:\\\\/\\\\/github\\\\.com\\\\/\\\\1/g"'

这样使用git url就可以看到仓库的http地址了。

打开的话就只需要执行git url | xargs open或者:

open `git url`

即可。

29. fatal: Could not open file .git/rebase-merge/done for reading: No such file or directory

git rebase --abort

30. 修改commit信息

未提交:

git commit --amend -m'xxxx'

已提交:

git rebase -i HEAD~n
git push --force

31. 了解.git目录

git cat-file -p xxxhashvalue

.git/objects目录保存了每次commit的信息 .git/refs目录保存了仓库的分支信息

32. git原理:git对象(git internals-git objects)

Git is essentially a simple key-value data store.

find .git/objects -type f echo 'test content' | git hash-object -w --stdin git cat-file -p xxxxhashvalue

33. 查看提交日志

git log --pretty=format:"[%h] %ae, %ar: %s" --stat

其中格式字符串的含义分别为:%h表示提交记录的 hash 值;%ae表示提交者的邮箱信息;%ar表示提交时间;%s表示提交日志信息。 git log格式的详细信息可以通过git help log然后搜索placeholder查看。

33. 快速编辑修改过的文件

vim -p `git log --name-only`

34. 快速比较两个分支同一个文件的不同

git diff mybranch..master -- myfile.cs
git diff mybranch master -- myfile.cs

35. git merge的标记说明

36. 合并多个 commit

git rebase --interactive HEAD~2

在打开的编辑窗口中将多次commit的信息修改成squash

37. 查看某个文件的commit信息

git shortlog -n -s <file_name>

37. 查看一个项目里 contributor 各自的提交数

git shortlog -sne

38. git 查看日志、提交记录和其他内容的技巧

  • git --no-pager 直接输出到终端而不是pager(默认为 less)查看
  • git diff --exit-code diff 有不同时输出 1,否则输出 0

例如检查当前模块版本是否有更新:

if git --no-pager diff --exit-code package.json; then
  echo 'No version update'
  exit 1
fi

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.