10 to save time and improve skills Git workflow

According to the manual, Git is defined as a fool content tracker, rich features, but some functionality is daunting. So, we just re-use several commands to be remembered that, while not fully use.

https://manpages.debian.org/stretch/git-man/git.1.en.html

Tip 1: Optimize Configuration

Git on a global, users and local level are highly configurable.

https://git-scm.com/docs/git-config

Find the order

Each setting can be overridden:

$CWD/.git/config 
    ▼ ▼ ▼ 
$HOME/.gitconfig` 
    ▼ ▼ ▼ 
$HOME/.config/git/config 
    ▼ ▼ ▼ 
/etc/gitconfig 

Modify settings

Use your favorite editor or CLI to edit any configuration files:

# 全局设置 
git config --global <keypath> <value> 
# 本地设置 
git config <keypath> <value> 

If the value contains a space character, you need to be enclosed in quotes.

Displays the current settings

# 显示当前设置及其来源 
git config --list --show-origin 

Some useful configuration

# 设定身份 
git config --global user.name "<your name>" 
git config --global user.email <your email> 
# 首选编辑器 
git config --global core.editor vim 
# 证书缓存 
# WINDOWS 
git config --global credential.helper manager 
# LINUX (超时时间——以秒为单位) 
git config --global credential.helper "cache --timeout=3600" 
# MACOS 
git config --global credential.helper osxkeychain 

https://git-scm.com/docs/gitcredentials

Tip 2: Alias ​​(alias)

Create an alias to save frequently used git command:

# 创建别名 
git config --global alias.<alias-name> "<git command>" 
# 使用别名 
git <alias-name> <more optional arguments> 

Some useful aliases

# 撤销上次提交 
git config --global alias.undo "reset --soft HEAD^" 
# 将暂存区更新修订到上次提交 (不改变提交信息) 
git config --global alias.amend "commit --amend --no-edit" 
# 压缩的状态输出 
git config --global alias.st "status -sb" 
# 用 GRAPH 为日志着色 
git config --global alias.lg "log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset'" 
# 删除所有已合并的分支 
git config --global alias.rmb "!git branch --merged | grep -v '*' | xargs -n 1 git branch -d" 
# 贡献排行 
git config --global alias.rank "shortlog -n -s --no-merges" 

Tip 3: Find and change Commits

Find information through Commits

# 通过 commit 信息查找 (所有分支) 
git log --all --grep='<search term>' 
# 通过 commit 信息查找 (包含 reflog) 
git log -g --grep='<search term>' 

By changing the look

# 通过更新的内容查找 
git log -S '<search term>' 

Find by date

# 通过日期范围查找 
git log --after='DEC 15 2019' --until='JAN 10 2020' 

Tip 4: Add a hunk

git add <filepath> All changes will not only add files, - path / -p parameter can also be an interactive scratch pad block.

# 补丁命令 
y = 暂存区块 
n = 不暂存这个区块 
q = 退出 
a = 暂存当前文件的此区块以及所有剩余区块 
d = 不暂存当前文件的此区块以及所有剩余区块 
/ = 查找区块 (正则表达式) 
s = 划分成更小的区块 
e = 手动编辑区块 
? = 打印帮助说明 
g = 选择要前往的区块 
j = 将区块设为未定,查看下一个未定区块 
J = 将区块设为未定,查看下一个区块 
k = 将区块设为未定,查看上一个未定区块 
J = 将区块设为未定,查看下一个区块 

https://git-scm.com/docs/git-add#Documentation/git-add.txt--i

Tip 5: Storage (stash) changes without submitting

stash the current changes temporarily shelved. Under its help, you can return to the index of the current state, and can later apply the changes have been stored.

By default, only the storage of the current track file changes, the new file will be ignored.

We can independently create and apply multiple stash.

https://git-scm.com/docs/git-stash

create

# 创建新的 STASH 
git stash 
# 创建新的 STASH (包含未追踪的更改) 
git stash -u/--include-untracked 
# 创建新的 STASH 并命名 
git stash save "<stash name>" 
# 交互式储藏 
git stash -p 

Set out

# 列出所有的 STASH (为其他命令提供"n") 
git stash list 

Browse

# 浏览 STASH 内容 
git stash show 
# 浏览 STASH 差异 
git stash show -p 

application

# 应用上一个 STASH (删除 stash) 
git stash pop 
# 应用上一个 STASH (保留 stash) 
git stash apply 
# 应用特定的 STASH (n = stash 列表序号) 
git stash pop/apply stash@{n} 
# 从 STASH 创建新的分支 (n = stash 列表序号) 
git stash branch <new branch name> stash@{n} 
# 从 STASH 应用单个文件 (n = stash 列表序号) 
git checkout stash@{n} -- <filepath> 

Clear up

# 删除特定的 STASH (n = stash 列表序号) 
git stash drop stash@{n} 
# 删除所有的 STASH 
git stash clear 

Tip 6: Dry run (Dry Run)

Many git operation may be destructive, for example, git clean -f will delete all untracked files, and can not be recovered.

To avoid such disastrous results appear, many commands are supported by dry-run, it can be checked before actually produce results. But unfortunately that option does not use exactly the same:

git clean -n/--dry-run 
git add -n/--dry-run 
git rm -n/--dry-run 
# GIT MERGE 模拟 DRY-RUN 
git merge --no-commit --no-ff <branch> 
git diff --cached 
git merge --abort 

Please note, git commit -n is not a dry-run! It is actually --no-verify, the role is to ignore all pre-commit / commit-msg githooks.

Tip 7: mandatory safety push

In the case of processing the old commit, create a new head from time to time and so it is easy to mess up branches. git push --force can cover the distance change, but should not do that!

git push --force is a destructive and dangerous operation because it unconditionally into effect, and will destroy all commit other submitters have already been pushed. This warehouse for other people's code is not necessarily fatal, but to change the history and influence others is not a good idea.

The better option is to use git push --force-with-lease.

git does not cover the upper reaches of the unconditionally remote repository, but check for remote changes are not available locally. If so, it will fail with a "stale info" message and tell us you need to run git fetch.

https://git-scm.com/docs/git-push#Documentation/git-push.txt---force-with-leaseltrefnamegt

Tips 8: Modify the commit message

Commit is immutable and can not be changed. But you can use a new revision of existing information commit commit, this will overwrite the original commit, so do not use it already pushed the commit.

git commit --amend -m "<new commit message>" 

https://git-scm.com/docs/git-commit#Documentation/git-commit.txt---amend

Tips 9: revision of history

Modify the code repository of history is not limited to modify the last submission information, can be modified using git rebase multiple submissions:

# 提交的范围 
git rebase -i/--interactive HEAD~<number of commits> 
# 该 hash 之后的所有提交 
git rebase -i/--interactive <commit hash> 

In the configuration editor reverse list all commit, like this:

# <command> <commit hash> <commit message> 
pick 5df8fbc revamped logic 
pick ca5154e README typos fixed 
pick a104aff added awesome new feature 

By changing the actual content editor, can provide a solution for git, to illustrate how to rebase:

# p, pick   = 使用提交而不更改 
# r, reword = 修改提交信息 
# e, edit   = 编辑提交 
# s, squash = 汇合提交 
# f, fixup  = 类似"squash",但是会丢弃提交信息 
# x, exec   = 运行命令 (其余行) 
# d, drop   = 移除提交 

After saving the editor, git will run it to rewrite history. e, edit pauses rebase, you can edit the current state of the code repository. Once that is done, run git rebase --continue.

If the problem (for example, merge conflicts) appear process, we need to start again, you can use git rebase --abort.

https://git-scm.com/docs/git-rebase

Skills 10: Archive trace file

You can compress the trace file specific reference to the use of different formats (zip or tar):

git archive --format <format> --output <filename> <ref> 

<Ref> may be a branch, commit hash or a label.

https://git-scm.com/docs/git-archive

Additional reminder: a single dash

There is a shortcut you can represent just used the branches: a single dash -

git checkout my-branch 
# 当前分支:my-branch 
<do some git operations, e.g. adding/commiting> 
git checkout develop 
# 当前分支:develop 
git merge - 
# 将 my-branch 合并到 develop 

Equivalent to a single dash @ {--1}.

Git - git-checkout Documentation

to sum up

Git There are many topics to talk about, here involves only scratched the surface. In another article, I want to show how to use git bisect effective for bad commit, or how to use the complete history of any operation by git git reflog.

Published 18 original articles · won praise 588 · Views 1.03 million +

Guess you like

Origin blog.csdn.net/hellozhxy/article/details/104750680