12 tips and tricks to make your Git experience more useful and powerful

Git, a distributed version control system, has become the source control default tool for the open source world. It was 12 years old on April 7. But what's even more frustrating about using Git is how much you need to know before you can use it more effectively. It's also a wonderful thing to use in Git, because nothing is more enjoyable than finding a new trick to simplify or increase the efficiency of your workflow.

To commemorate Git's 12th birthday, this article provides 12 tips and tricks to make your Git experience more useful and powerful, starting with some of the foundations you might miss and starting with some really powerful techniques!

1. Your ~/.gitconfig file

The first time you commit a repository modification with the git command, you may first see something like this:

*** Please tell me who you are.

Run

Git config --global user.email ""

Git config --global user.name "Your Name"

To set your account's default identity.

You may not realize that these commands are modifying the contents of the ~/.gitconfig file, which is a file for Git to store global configuration options. There are many things you can do with your ~/.gitconfig file, including defining aliases, permanently opening (or closing) certain command options, and modifying aspects of how Git works (eg, which diff algorithm git diff uses). Or what type of merge strategy is used by default). You can even include other configuration files on a conditional basis to a repository! Use "man git-config" to see all the details.

2. Your repository's .gitconfig file

In the previous tips, you may want to know what the -global flag does in the git config command. It tells Git to update the "global" configuration, which is the configuration found by ~/.gitconfig. Of course, having a global configuration represents a local configuration, and it is certainly enough that if you omit the -global option, git config will update the repository's own configuration, which is stored in .git/config.

The options set in .git/config override the corresponding settings in the ~/.gitconfig file. So, for example, if you need to use a different email address in a particular repository, you can run "git config user.email "". Then, you submit in this repository will use this email address that you individually configured. If you are working on an open source project with a working computer, but want to use a personal email address in this project, and other people still use the work mailbox in the main Git configuration, this is very useful.

Anything that can be set in ~/.gitconfig can be set in .git/config to make specific settings for this repository. In the following tips, when I mentioned adding something in your ~/.gitconfig file, it also shows that you can add it to the specific repository's .git/config to set that option.

Alias

An alias is another thing that you can do in your ~/.gitconfig file. It works like an alias on the shell command line - setting a new command name to call one or more other commands, which usually include some specific options or flags. Aliases are very effective for the long and complicated command lines you use regularly.

You can use the git config command to define aliases - for example, executing the "git config —global —add alias.st status" command will cause git st to do the same thing as doing git status — however, I found When defining aliases, it is usually easier to just edit directly in the ~/.gitconfig file.

If you choose to do this, you'll find that the ~/.gitconfig file is an INI file, and INI is a basic key-value pair file format with specific paragraphs. When you add an alias, you will change the [alias] paragraph. For example: To define the same git st alias as mentioned above, add the following code:

[alias]

St = status

(If you already have the [alias] paragraph, just add it to the second line in this paragraph)

4. Alias ​​in shell command

Aliasing is not just about running other Git subcommands - you can also define aliases that can run other shell commands. This is a good way to handle a repetitive, rare, and complex task: once you have thought of how to do it for the first time, use an alias to save this command. For example, I have several warehouses where I forked an open source project and made some changes locally that don't contribute to the project. During the ongoing development of the project I wanted to keep the latest version while keeping my local modifications. To complete this idea, I need to periodically merge these changes from the upstream repository to my fork - I define an alias "upstream-merge" to do this. The definition is as follows:

Upstream-merge = !"git fetch origin -v && git fetch upstream -v && git merge upstream/master && git push"

The "!" at the beginning of the alias definition tells Git to run this command through the shell. This example includes running some git commands, but defining aliases this way can run any shell command.

(Note: If you want to copy my upstream-merge alias, you will need to make sure you have a Git remote named upstream to specify this fork's upstream repository. You can use "git remote add upstream". "To add one."

5. Visual submission chart

If you are working on a project with many branch activities, it may sometimes be difficult to grasp all the work that is happening and their relevance. Various GUI tools allow you to figure out the profiles of different branches and submit records in a so-called "submit map." For example, the following is a screenshot of a memory card that I used to visualize using the GitLab Submit Graph Viewer:

If you are a user focused on the command line, you can not switch between multiple tools to cause distractions. This tool implements a graphical interface-like submit view on the command line. Get git's record with the –graph parameter:

The following command can get the same warehouse visualization fragment:

Git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' - -abbrev-commit --date=relative

The –graph option adds the chart to the left side of the log, the –abbrev-commit storage commit uses the SHA method, the –date=relative expression expresses the date in relative terms, and –pretty processes the custom format in bit format. I know the git lg alias, which is one of the 10 commands I run most often.

6. More elegant force-push

Sometimes, just as difficult as you can avoid using it is that you will find that you need to run git push –force to overwrite the history on your remote copy of the repository. You may have received some feedback. They will ask you to perform an interactive rebase, or you may have already messed up and want to hide evidence.

When someone else makes changes on the same branch of a remote copy of a warehouse, there is a risk of a forced push. Some submissions will be lost when you force the rewritten history to be pushed. This is why git push –force-with-lease appears – if the remote branch has been updated, it will not allow you to perform a push push, which will ensure that you do not discard the work of others.

7. git add -N

Have you used git commit -a to commit all your unfinished changes in one action, only to find that git commit -a ignores the newly added file after you push your commit? To solve this problem you can use git add -N ("notification") to tell Git that you want to include the newly added file in the commit before your first actual commit.

8. git add -p

A best practice is to make sure that each commit contains only one logical change when using Git – either fixing a bug or (implementing) a new feature. However, sometimes when you work, there will be more than one change submission in your repository. How do you separate things so that each commit only contains the appropriate modifications? Git add –patch to save!

This flag will cause the git add command to see all the changes in your working copy, asking if you would like to commit it, skip, or postpone the decision (there are other more powerful options that you can choose by running this command. ? to see). Git add -p is a magic tool to produce well-structured commits.

9. git checkout -p

Like git add -p , the git checkout command will use either the –patch or -p option, which will cause git to display each “bullet” change in the local working copy and allow the corresponding changes to be discarded—in short, to restore. Local working copy to the state before you change.

This is useful in some scenarios. For example, when you trace a bug, you introduce a bunch of debug log statements. After fixing the bug, you can use git checkout -p to remove all newly added debug logs, and then use git. Add -p to add bug fixes. Nothing is more satisfying than combining an excellent, well-structured submission!

10. Rebase with command execution

Some projects have a rule that every commit in the repository must be workable—that is, the code should be compilable on each commit, or running the test suite should not fail. When you work on a branch for a long time, but if you eventually need rebase for some reason, it is tedious to skip every rebase after the commit to ensure that you did not accidentally introduce an interrupt.

Fortunately, git rebase already supports the -x or -exec option. Git rebase -x This command will be run after each commit applied to rebase. So, for example, if you have a project where npm run tests will run your test suite, the git rebase -x npm run tests will run the test suite after each commit during rebase. This allows you to see if the test suite failed in any post-rebase commit, so you can make sure that the test suite still passes on every commit.

11. Guide based on time modification

Many Git subcommands accept a modified parameter to determine which part of the repository the command applies to, which may be a specific committed sha1 value, or the name of a branch, or a symbolic name such as HEAD (for current Check out the last commit of the branch. In addition to these simple forms, you can also attach a specified date or time as a parameter, which means "quote this time."

This feature can be very useful at certain times, such as when you deal with the latest bugs, and said to himself: "This function is clearly good yesterday, what changed in the end", do not stare at the full screen The output of the git log tries to figure out when the commit changes. You just run git diff HEAD@{yesterday} and you see all the changes since yesterday. This also applies to longer time periods (eg git diff HEAD@ {'2 months ago'}) and an exact date (eg git diff HEAD@{'2010-01-01 12:00:00'}).

You can also use these date-based modification parameters with any Git subcommands that use modification parameters. Details about which format to use are in the gitrevisions man page.

12. All-know reflog

Did you try to get rid of a commit on rebase and later found out that you need to keep some of this commit? You may think that these submissions have never been found, and you can only start from the beginning. This is not true, but if you submit it in your local working copy, the submission will go to the "Reference Log" and you can still access it.

Running git reflog will display a list of all activities for the current branch in your local working copy and provide you with a SHA1 value for each commit. Once you find the commit that you gave up on rebase, you can run git checkout to check out the commit, copy the information you need, and then run git checkout HEAD to return to the branch's latest commit.

The above is the entire content

We hope that at least one of these techniques will teach you some new knowledge about Git. Git is already 12 years old. What is your favorite skill in this continuous innovation and constantly adding new features?

Insulated Power Cable

Insulated Power Cable,Bimetallic Crimp Lugs Cable,Pvc Copper Cable,Cable With Copper Tube Terminal

Taixing Longyi Terminals Co.,Ltd. , https://www.longyiterminals.com