10 quick tips to boost your Git level "go"

Source: Internet
Author: User
Tags how to use git

Transferred from: Http://www.oschina.net/translate/10-tips-git-next-level

We have recently launched two tutorials: familiarity with Git's basic features and the ability to use GIT in a development team. The command we're talking about is enough for a developer to work with Git. In this article, we try to explore how to effectively manage your time and fully use the features that GIT provides.

Note: Some of the commands in this article contain portions of the brackets (for example:git add-p [file_name]). In these examples, you will insert the necessary numbers, identifiers, and so on, if there is no square brackets.

Mo Tian
Translated over 2 years ago

4 Person Top

Top translation of good Oh!

1. Git auto-complete

If you run a git command using a command-line tool, it's annoying to manually enter commands every time.
In order to solve this problem, you can enable the automatic completion of git, it takes only a few minutes to complete the work.

To get this script, run the following command under the UNIX system:

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

Then, add the following lines to your ~/.bash_profile file:

?
123 if-f ~/.git-completion.bash ]; then    . ~/.git-completion.bashfi

Although we have mentioned this earlier, the emphasis is not sufficient. If you want to use all of the features of Git,
You should definitely switch to the command line interface!

quentingui
translated 2 years ago

1 people top

 

top   Good translation!

2. Ignore files in Git

Are you annoying those compiled files (like. pyc) that appear in your git repository? Or have you had enough of that already added to the Git repository? Well, there's a way you can tell Git to ignore those specific files and folders. Just create a file named  .gitignore and then list the files and folders that you don't want Git to track. You can also add exceptions by using the exclamation point (!).

?
12345 * .pyc * .exe my_db_config /  !main.pyc
Open source China Anonymous member
translated 2 years ago

0 Human top

 

top   Good translation!

other translations (1)
3. Who messed up my code?

When things go wrong, it is one of human nature to accuse others first. If your product server hangs up, using the git blame command can easily find the culprit. This command can show the author of each line in the file, the latest change submission, and the time of submission.

?
1 git blame [file_name]

/p>

In the following you can see how the commands are searched in a larger directory.

0x0bject
translated 2 years ago

1 Human Top

 

top   Good translation!

4. View Warehouse History

Previous section We have learned how to use git log, but there are three options that you should know about. The

  • --oneline-compression mode, which displays a streamlined commit hash code and commit information next to each submission, in one line.

  • --graph-graphics mode, which draws a historical information representation of the text format on the left side of the output. This option is not available if you are viewing the history of a single branch.

  • --all-Show history for all branches

When you combine these options, the output looks like this:

Open source China Anonymous member
translated 2 years ago

2 people top

 

top   Good translation!

5. Never lose track of commit

If you accidentally commit something you don't want, you have to do a mandatory reset to revert to the previous state. Then you realize that in the process you lose some other information and want to get it back, or at least take a look at it. That's what git reflog can do.

A simple git log command can show you the last commit, as well as its father, his father's father, and so on. Git Reflog lists a series of commits that the head once pointed to. Understand that they exist only in your native computer, not part of your version repository, and are not included in push and merge operations.

If I run the git log command, I can see some commits, all of which are part of my repository:

However, a git reflog command shows a commit (b1b0ee9& #8211; Email protected]{4}), which I lost when I was forced to reset:

Lwei
Translated over 2 years ago

1 Person top

Top translation of good Oh!

6. Partial changes to the staging file

In general, it is good practice to create an attribute-based commit, meaning that each commit must represent the creation of a new feature or a bug fix. What if you fixed two bugs, or added multiple new features but didn't commit the changes? In this case, you can put these changes in one commit. But a better approach would be to put the file on hold (stage) and submit it separately.

For example, you have made several changes to a file and want to submit them separately. In this case, you can add the-p parameter to the add command

?
1 git add -p [file_name]

Let's demonstrate that 3 lines of text are added to the file_name file, but only the first and third lines are submitted. First look at the results of the Git diff display:

And then see what is the Add-p parameter in the add command?

It seems that Git assumes that all changes are directed at the same thing, so it puts them in a single block. You have several options as follows:

    • Enter Y to temporarily save the block

    • Input n is not staged

    • Enter E to edit the block manually

    • Enter D to exit or go to the next file

    • Enter S to split the block

In our example, we end up with the desire to split into smaller parts, and then selectively add or omit portions of them.

As you can see, we added the first and third rows and ignored the second line. After that you can view the status of the warehouse and submit it.

Guest
Translated over 2 years ago

1 Person top

Top translation of good Oh!

7. Compress multiple commits

When you submit code for code review or create a pull request (which happens frequently in open source projects), your code is asked to make some changes before it is accepted. You have made a change and you have not been asked to change it again until the next review. You've got some extra commits before you know it's going to change again. Ideally, you can compress multiple commits into one using the rebase command.

?
1 git rebase -i HEAD~[number_of_commits]

If you want to compress the last two commits, you need to run the following command.

?
1 git rebase -i HEAD~2

When you run the command, you'll see an interactive interface that lists a number of commits that let you choose which ones need to be compressed. Ideally, you choose the last commit and compress the other old commits.

You will then be asked to enter the submission information for the new commit. This process essentially overrides your commit history.

Lwei
Translated over 2 years ago

1 Person top

Top translation of good Oh!

8. Stash UNCOMMITTED changes

You are modifying a bug or feature and are suddenly asked to show your work. And the work you're doing is not enough to submit, and you won't be able to show it at this stage (before you go back to the change). In this case, git stash can help you. Stash essentially takes all the changes and stores them for future use. Stash your changes, you simply run the following command-

?
1 git stash

To check the Stash list, you can run the following command:

?
1 git stash list

If you want to release stash and resume uncommitted changes, you can apply stash:

?
1 git stash apply

In the screen, you can see that each stash has an identifier, a unique number (although in this case we only have one stash). If you only want to leave the apply stash, you should add a specific identifier to apply:

?
1 git stash apply[email protected]{2}

No if
Translated over 2 years ago

0 Person Top

Top translation of good Oh!

9. Check for missing commits

Although Reflog is the only way to check for lost submissions. But it is not adapted for use in large warehouses. That's when the fsck (File system detection) command comes up.

?
1 git  fsck  --lost-found

 

Here you can see the dropped submissions. You can revert to the previous commit by running git show [Commit_hash] to view post-commit changes or run git merge [Commit_hash]. The

Git fsck has advantages over Reflog. Let's say you delete a remote branch and then close the warehouse. With fsck you can search for and recover deleted remote branches.

 

Fall the great
translated 2 years ago

0 Human Top

 

top   Good translation!

Cherry Pick

I left the most elegant git command at the end. The Cherry-pick command is my favorite git command so far, both because of its literal meaning and because of its functionality.

In short, Cherry-pick is to pick up a separate commit from a different branch and merge it with your current branch. If you are dealing with two or more branches in parallel, you may find a bug in all branches. If you resolve it in a branch, you can use the Cherry-pick command to commit it to another branch without messing up other files or commits.

Let's imagine a scenario that would need it. I now have two branches, and I want to cherry-pick b20fd14:cleaned junk this commit to the other one.

I switch to the branch that I want to be applied to by Cherry-pick, and then run the following command:

?
1 git cherry - pick [commit_hash]

 

Although we have completed a clean cherry-pick this time, you should also realize that this command may conflict. So be careful when using it.

htfy96
translated 2 years ago

0 human top

 

top   translation is good!

Summarize

At the end of the article, I think these techniques will elevate your Git level to a new height. Git is the best, and it almost does what you can think of. Therefore, you should always challenge your git level. In the end you will probably learn something new.

10 quick tips to boost your Git level "go"

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.