Chinaunix首页 | 论坛 | 博客
  • 博客访问: 629357
  • 博文数量: 85
  • 博客积分: 1306
  • 博客等级: 中尉
  • 技术积分: 990
  • 用 户 组: 普通用户
  • 注册时间: 2008-03-28 11:23
个人简介

嵌入式视频行业。

文章分类

全部博文(85)

文章存档

2015年(7)

2014年(5)

2013年(41)

2012年(11)

2011年(1)

2010年(3)

2008年(17)

分类: LINUX

2014-08-18 13:42:18

If you run Git commands through the command line, it’s a tiresome task to type in the commands manually every single time. To help with this, you can enable auto completion of Git commands within a few minutes.

To get the script, run the following in a Unix system:


点击(此处)折叠或打开

  1. cd ~
  2.     curl https://raw.github.com/git/git/master/contrib/completion/git-completion.bash -o ~/.git-completion.bash


Next, add the following lines to your~/.bash_profilefile:

点击(此处)折叠或打开

点击(此处)折叠或打开

  1. if [ -f ~/.git-completion.bash ]; then
  2.     . ~/.git-completion.bash
  3.     fi

Although I have mentioned this earlier, I can not stress it enough: If you want to use the features of Git fully, you should definitely shift to the command line interface!

2. Ignoring Files in Git

Are you tired of compiled files (like.pyc) appearing in your Git repository? Or are you so fed up that you have added them to Git? Look no further, there is a way through which you can tell Git to ignore certain files and directories altogether. Simply create a file with the name.gitignoreand list the files and directories that you don’t want Git to track. You can make exceptions using the exclamation mark(!).


点击(此处)折叠或打开

  1. *.pyc
  2. *.exe
  3. my_db_config/
  4. !main.pyc

3. Who Messed With My Code?

It’s the natural instinct of human beings to blame others when something goes wrong. If your production server is broke, it’s very easy to find out the culprit — just do agit blame. This command shows you the author of every line in a file, the commit that saw the last change in that line, and the timestamp of the commit.

1
git blame [file_name]

git blame demonstration

And in the screenshot below, you can see how this command would look on a bigger repository:

git blame on the ATutor repository

4. Review History of the Repository

We had a look at the use ofgit login a previous tutorial, however, there are three options that you should know about.

  • --oneline– Compresses the information shown beside each commit to a reduced commit hash and the commit message, all shown in a single line.
  • --graph– This option draws a text-based graphical representation of the history on the left hand side of the output. It’s of no use if you are viewing the history for a single branch.
  • --all– Shows the history of all branches.

Here’s what a combination of the options looks like:

Use of git log with all, graph and oneline

5. Never Lose Track of a Commit

Let’s say you committed something you didn’t want to and ended up doing a hard reset to come back to your previous state. Later, you realize you lost some other information in the process and want to get it back, or at least view it. This is wheregit reflogcan help.

A simplegit logshows you the latest commit, its parent, its parent’s parent, and so on. However,git reflogis a list of commits that the head was pointed to. Remember that it’s local to your system; it’s not a part of your repository and not included in pushes or merges.

If I rungit log, I get the commits that are a part of my repository:

Project history

However, agit reflogshows a commit (b1b0ee9–HEAD@{4}) that was lost when I did a hard reset:

Git reflog

6. Staging Parts of a Changed File for a Commit

It is generally a good practice to make feature-based commits, that is, each commit must represent a feature or a bug fix. Consider what would happen if you fixed two bugs, or added multiple features without committing the changes. In such a situation situation, you could put the changes in a single commit. But there is a better way: Stage the files individually and commit them separately.

Let’s say you’ve made multiple changes to a single file and want them to appear in separate commits. In that case, we add files by prefixing-pto our add commands.

1
git add-p [file_name]

Let’s try to demonstrate the same. I have added three new lines tofile_nameand I want only the first and third lines to appear in my commit. Let’s see what agit diffshows us.

Changes in repo

And let’s see what happes when we prefix a-pto ouraddcommand.

Running add with -p

It seems that Git assumed that all the changes were a part of the same idea, thereby grouping it into a single hunk. You have the following options:

  • Enter y to stage that hunk
  • Enter n to not stage that hunk
  • Enter e to manually edit the hunk
  • Enter d to exit or go to the next file.
  • Enter s to split the hunk.

In our case, we definitely want to split it into smaller parts to selectively add some and ignore the rest.

Adding all hunks

As you can see, we have added the first and third lines and ignored the second. You can then view the status of the repository and make a commit.

Repository after selectively adding a file

7. Squash Multiple Commits

When you submit your code for review and create a pull request (which happens often in open source projects), you might be asked to make a change to your code before it’s accepted. You make the change, only to be asked to change it yet again in the next review. Before you know it, you have a few extra commits. Ideally, you could squash them into one using therebasecommand.

1
git rebase-i HEAD~[number_of_commits]

If you want to squash the last two commits, the command that you run is the following.

1
git rebase-i HEAD~2

On running this command, you are taken to an interactive interface listing the commits and asking you which ones to squash. Ideally, youpickthe latest commit andsquashthe old ones.

Git squash interactive

You are then asked to provide a commit message to the new commit. This process essentially re-writes your commit history.

Adding a commit message

8. Stash Uncommitted Changes

Let’s say you are working on a certain bug or a feature, and you are suddenly asked to demonstrate your work. Your current work is not complete enough to be committed, and you can’t give a demonstration at this stage (without reverting the changes). In such a situation,git stashcomes to the rescue. Stash essentially takes all your changes and stores them for further use. To stash your changes, you simply run the following-

1
git stash

To check the list of stashes, you can run the following:

1
git stashlist

Stash list

If you want to un-stash and recover the uncommitted changes, you apply the stash:

1
git stashapply

In the last screenshot, you can see that each stash has an indentifier, a unique number (although we have only one stash in this case). In case you want to apply only selective stashes, you add the specific identifier to theapplycommand:

1
git stashapplystash@{2}

After un-stashing changes

9. Check for Lost Commits

Althoughreflogis one way of checking for lost commits, it’s not feasible in large repositories. That is when thefsck(file system check) command comes into play.

1
git fsck--lost-found

Git fsck results

Here you can see a lost commit. You can check the changes in the commit by runninggit show [commit_hash]or recover it by runninggit merge [commit_hash].

git fsckhas an advantage overreflog. Let’s say you deleted a remote branch and then cloned the repository. Withfsckyou can search for and recover the deleted remote branch.

10. Cherry Pick

I have saved the most elegant Git command for the last. Thecherry-pickcommand is by far my favorite Git command, because of its literal meaning as well as its utility!

In the simplest of terms,cherry-pickis picking a single commit from a different branch and merging it with your current one. If you are working in a parallel fashion on two or more branches, you might notice a bug that is present in all branches. If you solve it in one, you cancherry pickthe commit into the other branches, without messing with other files or commits.

Let’s consider a scenario where we can apply this. I have two branches and I want tocherry-pickthe commitb20fd14: Cleaned junkinto another one.

Before cherry pick

I switch to the branch into which I want tocherry-pickthe commit, and run the following:

1
git cherry-pick [commit_hash]

After cherry pick

Although we had a cleancherry-pickthis time, you should know that this command can often lead to conflicts, so use it with care.

Conclusion

With this, we come to the end of our list of tips that I think can help you take your Git skills to a new level. Git is the best out there and it can accomplish anything you can imagine. Therefore, always try to challenge yourself with Git. Chances are, you will end up learning something new!

阅读(1138) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~