Git is so popular around the developer world, as well as Github. Nowadays almost every developer could use git a bit. It was hard to imagine this situation in several years ago.
To master your development tool would help you focus on development itself. And provide more information when hacking code.
Git has so many commands, and each commands have many parameters. It is common that people says “Oh! I don’t know we can use Git like that!”. This article is for sharing some commands I use often. Hope it helps, and I hope you can share your tips as well.
This is most basic. Make your own
~/.gitconfig. I did, and put lots of alias into it. Part of my
tig is a very useful command line interactive tool. Even in Mac you can install it via
In Git, you can specify a range by this format
<HASH1>..<HASH2>, for example
Sometimes you will co-work with people who cannot access full repository. We can send some changes to him by patches. Because Git is patch-based tool, manipulating patches is natural for Git.
To format patches, we should specify a range to command.
In the other side, people can apply that patch via
The benefit of
am is that it remains commit message.
If you gonna to share a snapshot to people who cannot access full repository. For example, you co-work with another company, and they want source code in a specific version.
of course you can use tag, if you manage tags in proper way.
git log. It can provide many information in pretty format.
Not only to see the content of one commit, we can also see full content of one file in specific commit.
Sometime when I want review a Pull-Request. But you know, usually if you cannot see the whole file, you cannot get overview of the change, then it is hard to understand “Is this change safe?”.
I will use this command to see diff in whole file.
10000 is just a large-enough number. of course you can adjust it. Sometime I use vim to review patch
The basic usage of diff is to see different conent between two commits.
To see What changed in a specific file, between two commits?
If we want to see What files were changed, between remote master and my current work tree?
If you found there are too many small object files under
.git/objects/, this command could pack them into one large file.
ps. Git is more like a object filesystem. The way Git works is to manage many objects.
If you want to clean up EVERYTHING doesn’t be tracked by git, such as generated files.
Be careful, I usually use command and mis-cleaned my config files.(orz)
This command will save your ass in some day.
Sometimes you use
reset to change your HEAD, or to delete a branch. We know the content still exist in the git repository, we cannot get it back because we don’t know its hash.
That is why git reflog matters. It records every action you did in your git repository, so you can get everything back.
This is a bit dangerous.
If you found your git repository is huge, full of objects you really don’t need. The command will prune not-tracked-objects. For example, if you had ever fetch objects from another remote server, but you don’t need them anymore. Even if you delete every branches in your repo, those objects still exists in your git repo. (a bit like dangling object)
This command will clean them. And it definitely effects reflog, so pleae be careful for this command.
Well. I don’t wanna talk about
rebase is too IMPORTANT so it should be a standalone topic. In my personal opinion, a people don’t udnerstand Git until who know how to use
git rebase -i.
rebase is not only doing rebase, but can also edit previous commits.(remove, edit, squash). To use
rebase in proper way will make you commits looks perfect.
This is one of most useful git-rebase command. Please read git help to understand how to use git rebase, if you don’t know about it.
At least, in here I want introduce a common situation in development process.
- You start work from commit
foobar, and you create 5 commits in branch
next. You are in branch
- Some people works on
masterbranch and have progress.
masterbranch got some commits.
- Your colleage create new branch
topic, and he gave 3 commits into it.
If you wanna get his effort to your branch. It means I wanna develop base on my colleague’s work. Then we can use rebase like this
Then the result looks like