Some tricks with Git

この記事は公開されてから半年以上経過しています。情報が古い可能性がありますので、ご注意ください。

As a Developer, we love to explore how other developers work. So now, let’s take a look at some of the features that you might not familiar with, or at the possibilities that git provides!

Git is a distributed version control system. It is a open-source code management tool. Everything you do is reversible. It doesn’t force you to work in a particular way; makes it easy to experiment with new ideas not worry about breaking anything. It is easy to get the hang of as well. So get ready!

Aliases

Git lets you create aliases, which are shortcuts to other commands that will literally save you minutes a year in saved keystrokes. Stop wasting time typing long commands and make yourself a few useful aliases. Aliases can be made by adding them to your .gitconfig file or using the command-line

git config --global alias.<NAME> "<COMMAND>"

Typing git checkout every time you wanted to see a new branch over and over has been a bit verbose.

git config --global alias.co checkout

Stage your changes and then use git co branch.

Let’s create a few aliases for our most common commands:

[alias]
	
ds = diff --staged
st = status -sb
fup = log --since '1 day ago' --oneline --author <YOUR_EMAIL>

# pretty one-line log with tags, branches and authors
ls = log --pretty=format:"%C(yellow)%h %C(blue)%ad%C(red)%d %C(reset)%s%C(green) [%cn]" --decorate --date=short

# a verbose ls, shows changed files too
lsv = log --pretty=format:"%C(yellow)%h %C(blue)%ad%C(red)%d %C(reset)%s%C(green) [%cn]" --decorate --date=short --numstat

# some resets without explanation
r = reset
r1 = reset HEAD^
r2 = reset HEAD^^
rh = reset --hard
rh1 = reset HEAD^ --hard
rh2 = reset HEAD^^ --hard

# basic shortcuts
cp = cherry-pick
cl = clone
ci = commit
co = checkout
br = branch 
diff = diff --word-diff
dc = diff --cached

# stash shortcuts
sl = stash list
sa = stash apply
ss = stash save

# log related - thanks to @mwd410
l = log 
lh = log --graph
la = !git lh --date-order --all 2> /dev/null
lb = log --graph --simplify-by-decoration
lba = !git lb --all 
h = !git --no-pager log --graph -n 15
a = !git --no-pager la -n 15

Git includes a long list of configuration options, all of which can be found in the git-config manual. Note that storing your global configurations in a plaintext file makes it incredibly easy to transfer your settings to a new Git installation: just copy ~/.gitconfig onto your new machine.

Branches sorted by last commit

The for-each-ref command will output a list for each branch and show the reference information for the last commit, or sort the list by date. I highly recommend making it an alias instead of typing this command each time.

git config --global alias.latest_commit "for-each-ref --sort=-committerdate --format='%(committerdate:short) %(refname:short) [%(committername)]'"

then use;

git latest_commit

Creating new branch with no history

There is a very simple way to create a new branch in your repo that essentially has no history.

git checkout --orphan 

Checkout to your last branch

Let’s say we had to make a quick fix to our project, and you don’t want that in your feature branch. You’ll need to stash your current changes, and checkout your master branch and make the fix there. Then switch back to your feature branch again.

You’ll never have to remember what you named that feature branch. There is a quick shortcut for switching to your last branch:

git checkout -

Branches that are merged or not

If you want to see which local branches you have that are merged into the branch you are currently on, then all you need is:

git branch --merged

otherwise

git branch --no-merged

You can mash this up with delete.

git branch --merged | xargs git branch -d

other wise

git branch --no-merged | xargs git branch -d

Fetch a file from another branch without switching branches

Let’s say that you have a few branches that have various changes you’ve made. If you have changes in a file in some distant branch that you want to bring into your current working branch, then you could simply merge a single file in your current branch from another:

git checkout  -- path/to/file.rb

Merging branches without checkout

Let’s say we are working in a develop branch which is ahead of master by several commits. And we have a large repo so checkouts are time-consuming, or our compiler relies on timestamps and may think that a lot of files are dirty and need a rebuild. So we need another way .We want to merge develop to master.

git fetch <remote> <sourceBranch>:<destinationBranch>

then master will be fast-forwarded to develop.

Interactive adding

You are certainly familiar with accidentally modifying a single file for two different reasons without committing in between. Then we will use interactive adding. When you add a file with the -p command, you will be prompted at each logical change (i.e., successively edited lines will be grouped together).

git add -p 

Find something in your entire git history

Sometimes, you need to hunt down a line of code you know you wrote but you don’t find with plain old grep — maybe someone deleted or changed it with a commit. You remember some parts of it but have no idea where and when you committed it. Fortunately git has your back on this. Let’s fetch all commits ever then use git’s internal grep subcommand to look for your string:

git rev-list --all | xargs git grep '' 
git rev-list --all | xargs git grep -F '' # if you don't want to use regex

Git can autocorrect you

git config --global help.autocorrect 1

Get a color-coded, one-line-per-commit log showing branches and tags

git log --oneline --decorate

Finding whose to blame

Often it can be useful to find out who changed a line of code in a file.

git blame FILE

Recovering a lost branch

If you delete a branch with -D you can recreate it with:

git fsck --lost-found

It is dangling commit here is the lost HEAD (it will only be the HEAD of the deleted branch as the HEAD^ is referred to by HEAD so it’s not dangling).

There is so much more you can learn about Git, hopefully you’ve learned extra commands to help you manage your project more smartly. With all of these convenient features, it’s easy to get so caught up in designing the perfect workflow that you lose sight of Git’s underlying purpose. If you ever find that Git is causing more harm than good, don’t be scared to drop some of the advanced features and go back to the basics. But don’t stop here; check out other resources to become a Git master! And the most powerful command as you learn; man git--.

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.