未加星标

5 Git Fundamentals

字体大小 | |
[系统(linux) 所属分类 系统(linux) | 发布者 店小二04 | 时间 2016 | 作者 红领巾 ] 0人收藏点击收藏
5 Git Fundamentals

I’m starting a blog series on some of git tips and tricks that I’ve learned during the half-decade that I’ve been using it. We’ll skip the everyday basics push, pull, log, status, diff, commit and start off with the fundamentals while connecting them holistically.

Let’s begin by creating a .gitconfig file in your home directory. This file is the basis for all your git configurations, customizations, and aliases. In case you’re curious, I have posted a link to mine at the end.

[user]
email = [email protected]
name = Jorge Yau [core]
editor = vim
ignorecase = false [alias]
g = grep --break --heading --line-number [grep]
lineNumber = true
extendRegexp = true [push]
default = simple [color "diff"]
meta = yellow bold ul
frag = cyan bold ul
old = red bold
new = green bold [color "status"]
added = green bold
changed = yellow bold
untracked = red bold

Once the file exists, git will preload and use these configurations for all further commands. Try running a git diff inside a repo, and see how the colors have changed.

I also have included a git alias, which we’ll see further down. An alias is basically a copy-paste shorthand for a command. In this case, git grep can be executed as git g . Additionally git grep will include the line number and use extended regex by default.

# So this longer command:
$ git grep --extended-regexp --line-number --break --heading # Is the same as:
$ git g

Most git commands have anywhere from 20 to 30 flags and options ― check out the official documentation to read more. For now, we’ll keep it simple.

While gitignore files are ubiquitous, did you know that you can globally ignore file patterns by creating a .gitignore_global file in your home directory and adding it to your .gitconfig under the core section. I recommend putting software remnants, IDE and OS related files here.

.idea*
**~
.DS_Store

Then either add this line to your .gitconfig file:

[core]
excludesfile = ~/.gitignore_global

Or run this command, which automatically appends it to your .gitignore

git config --global core.excludesfile ~/.gitignore_global

The git ls-files command is without a doubt one of the most useful git utility commands. Very frequently, I use it in combination with grep and sed to perform a bulk find-and-replace or code trimming ― occasionally using regex with captured groups (more on that in the future). However, I also want to ignore third-party files such as the node_modules directory which is just not as sleek with regular grep ―though ack is much sexier .

$ grep -E debugger . -rl --exclude-dir={*components*,*node_modules*} $ ack debugger -l --ignore-dir={components,node_modules}

But what if I need to list all files in the repository ― there’s no simple way to accomplish with regular bash. Well, thankfully git ls-files will list all files in the repository while also excluding the file patterns in your gitignore.

$ git ls-files .bowerrc
.dockerignore
.eslintignore
.eslintrc.yml
.gitignore
.gitlab-ci.yml
.scss-lint.yml
Dockerfile
README.md
_deploy_to_qa.sh
...

You can then pipe in the results to grep or ack to filter for certain files. In this case, I’m filtering for all javascript files in my repository ― the $ is to make sure that .json files are excluded.

$ git ls-files | grep -E .js$
$ git ls-files | ack .js$

If you’re familiar with grep, you can use git grep as a handy alternative. It will ignore files in your .gitignore by default when grepping through files. Remember the configuration changes that we made above to your.gitconfig? Well they will apply additional set of styling and helpful defaults.

$ git grep 'debugger' # Show the count of occurrences for each file.
$ git grep --count 'debugger'
$ git grep -c 'debugger' # Set the number of lines before and after for each result.
$ git grep --before-context 5 --after-context 5 'debugger'
$ git grep -B 5 -A 5 'debugger' # Use the quiet flag for automated scripts. Exits 0 if match found.
# It's very useful for CI, linting, and git hooks.
$ git grep --quiet debugger
$ git grep -q debugger

Git and grep are often paired together in many other commands. If you want to search through your commit messages you can use git log --grep .

# Search for 'bug' in the git commit message history.
git log --grep 'bug' # I recommend using this flag to ignore merge commits.
git log --no-merges --grep 'bug' # Refine your searches to a particular author.
git log --grep 'bug' --author 'codenameyau'

I have an alias which I use to print the shortened commit hash, the date, the author, and the commit message in one line. You can use it like so:

# Put this inside your ~/.gitconfig
[alias]
search = log --no-merges -i -E --pretty='%h (%ad) - [%an] %s %d' --date=format:'%b %d %Y' --grep
# Then use the command like so:
$ git search 'bug' # Pipe it to wc to get the number of occurences for 'fix' or 'bug'.
$ git search 'fix|bug' | wc -l

Searching through commit messages from what I’ve seen is a seldomly used but exceptionally useful feature for tracing feature developing, tracking down bugs, and analyzing your codebase. Here is git search in action.


5 Git Fundamentals
If you like the colors, check out my custom bash prompt alternative tozsh.

In order for git search to be useful, it’s best to have contextual but concise commit message under 80 characters. One very helpful convention is to include a hash (#) followed by the issue number. This will automatically link the commit to the issue for repository managers like GitHub, and it makes documenting and investigating issues much more feasible.

$ git commit -m "#123 - Fix bug with useful git commit message"

Bonus tip: once you’ve grasped the basic fundamentals, definitely check out the git-extras repository. It is an extension of git with extremely useful commands ( I’m looking at you obliterate ) written in bash.

https://github.com/tj/git-extras/b

本文系统(linux)相关术语:linux系统 鸟哥的linux私房菜 linux命令大全 linux操作系统

主题: GitDockerJavaScriptJavaGitHubADM
分页:12
转载请注明
本文标题:5 Git Fundamentals
本站链接:http://www.codesec.net/view/484473.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 系统(linux) | 评论(0) | 阅读(25)