banner



How To Configure Git Bash On Windows

And then far, nosotros've covered the basics of how Git works and how to employ it, and nosotros've introduced a number of tools that Git provides to help you use it easily and efficiently. In this chapter, we'll encounter how you can make Git operate in a more customized fashion, by introducing several important configuration settings and the hooks organization. With these tools, it's like shooting fish in a barrel to get Git to work exactly the way yous, your visitor, or your group needs it to.

Git Configuration

Every bit you read briefly in Getting Started, you can specify Git configuration settings with the git config control. One of the starting time things you did was set up your name and email address:

            $ git config --global user.proper noun "John Doe" $ git config --global user.email johndoe@example.com          

At present y'all'll learn a few of the more interesting options that you can set in this mode to customize your Git usage.

Get-go, a quick review: Git uses a series of configuration files to determine non-default beliefs that you may want. The first identify Git looks for these values is in the system-wide [path]/etc/gitconfig file, which contains settings that are applied to every user on the system and all of their repositories. If you laissez passer the pick --arrangement to git config, it reads and writes from this file specifically.

The next place Git looks is the ~/.gitconfig (or ~/.config/git/config) file, which is specific to each user. You can make Git read and write to this file past passing the --global option.

Finally, Git looks for configuration values in the configuration file in the Git directory (.git/config) of whatever repository you're currently using. These values are specific to that unmarried repository, and represent passing the --local choice to git config. If you lot don't specify which level you want to work with, this is the default.

Each of these "levels" (organization, global, local) overwrites values in the previous level, so values in .git/config trump those in [path]/etc/gitconfig, for instance.

Note

Git'south configuration files are evidently-text, so you lot tin can also set these values by manually editing the file and inserting the correct syntax. It's generally easier to run the git config command, though.

Basic Client Configuration

The configuration options recognized past Git fall into ii categories: client-side and server-side. The majority of the options are client-side — configuring your personal working preferences. Many, many configuration options are supported, but a big fraction of them are useful merely in certain edge cases; we'll comprehend just the about common and useful options here. If you want to see a list of all the options your version of Git recognizes, you can run:

This control lists all the available options in quite a flake of detail. You can also find this reference material at https://git-scm.com/docs/git-config.

core.editor

Past default, Git uses whatever you've set as your default text editor via one of the shell environment variables VISUAL or EDITOR, or else falls dorsum to the vi editor to create and edit your commit and tag letters. To change that default to something else, y'all tin apply the core.editor setting:

                $ git config --global cadre.editor emacs              

Now, no matter what is fix equally your default shell editor, Git will burn down up Emacs to edit messages.

commit.template

If you set this to the path of a file on your system, Git will employ that file as the default initial message when you lot commit. The value in creating a custom commit template is that you can use information technology to remind yourself (or others) of the proper format and style when creating a commit message.

For example, consider a template file at ~/.gitmessage.txt that looks like this:

                Subject line (attempt to keep under l characters)  Multi-line clarification of commit, feel gratuitous to exist detailed.  [Ticket: X]              

Note how this commit template reminds the committer to keep the subject line brusque (for the sake of git log --oneline output), to add further detail under that, and to refer to an effect or problems tracker ticket number if one exists.

To tell Git to apply it as the default message that appears in your editor when you run git commit, set the commit.template configuration value:

                $ git config --global commit.template ~/.gitmessage.txt $ git commit              

Then, your editor will open to something like this for your placeholder commit bulletin when yous commit:

                Subject line (try to continue under 50 characters)  Multi-line description of commit, feel free to be detailed.  [Ticket: X] # Please enter the commit message for your changes. Lines starting # with '#' will be ignored, and an empty message aborts the commit. # On branch master # Changes to be committed: #   (utilise "git reset Head <file>..." to unstage) # # modified:   lib/test.rb # ~ ~ ".git/COMMIT_EDITMSG" 14L, 297C              

If your team has a commit-bulletin policy, then putting a template for that policy on your system and configuring Git to use it by default can assistance increase the hazard of that policy beingness followed regularly.

This setting determines which pager is used when Git pages output such as log and unequal. You can gear up information technology to more or to your favorite pager (by default, it's less), or you can plow it off by setting it to a blank cord:

                $ git config --global cadre.pager ''              

If you run that, Git volition folio the unabridged output of all commands, no matter how long they are.

user.signingkey

If you're making signed annotated tags (equally discussed in Signing Your Work), setting your GPG signing key as a configuration setting makes things easier. Fix your fundamental ID similar so:

                $ git config --global user.signingkey <gpg-central-id>              

Now, you tin can sign tags without having to specify your central every time with the git tag command:

core.excludesfile

You tin put patterns in your project's .gitignore file to accept Git not see them as untracked files or try to phase them when you run git add together on them, as discussed in Ignoring Files.

But sometimes you want to ignore sure files for all repositories that yous work with. If your computer is running macOS, you're probably familiar with .DS_Store files. If your preferred editor is Emacs or Vim, you know about filenames that end with a ~ or .swp.

This setting lets you write a kind of global .gitignore file. If you create a ~/.gitignore_global file with these contents:

…and yous run git config --global core.excludesfile ~/.gitignore_global, Git volition never again bother you about those files.

help.autocorrect

If you lot mistype a command, it shows you something like this:

                $ git chekcout master git: 'chekcout' is not a git control. See 'git --assistance'.  The near similar command is     checkout              

Git helpfully tries to figure out what you meant, but it nonetheless refuses to practise information technology. If you set help.autocorrect to 1, Git will actually run this control for you:

                $ git chekcout chief WARNING: You called a Git command named 'chekcout', which does not exist. Continuing under the supposition that you lot meant 'checkout' in 0.1 seconds automatically...              

Note that "0.one seconds" business. assistance.autocorrect is really an integer which represents tenths of a second. So if you set it to fifty, Git will give y'all 5 seconds to change your mind before executing the autocorrected control.

Colors in Git

Git fully supports colored terminal output, which greatly aids in visually parsing control output rapidly and easily. A number of options can help you set the coloring to your preference.

colour.ui

Git automatically colors about of its output, but at that place's a master switch if you don't similar this behavior. To turn off all Git's colored terminal output, do this:

                $ git config --global color.ui faux              

The default setting is motorcar, which colors output when it'due south going straight to a last, but omits the color-control codes when the output is redirected to a pipage or a file.

You can also prepare information technology to always to ignore the divergence between terminals and pipes. Y'all'll rarely want this; in most scenarios, if you want color codes in your redirected output, you can instead pass a --color flag to the Git command to force it to use colour codes. The default setting is about ever what yous'll want.

color.*

If you desire to be more specific near which commands are colored and how, Git provides verb-specific coloring settings. Each of these can be set to true, simulated, or e'er:

color.branch color.diff color.interactive color.status

In add-on, each of these has subsettings you lot can use to set up specific colors for parts of the output, if you desire to override each color. For example, to fix the meta information in your diff output to blue foreground, black background, and assuming text, you tin run:

                $ git config --global color.unequal.meta "blue blackness bold"              

You can set the color to any of the following values: normal, black, red, greenish, yellowish, blueish, magenta, cyan, or white. If y'all desire an attribute similar bold in the previous example, you can choose from assuming, dim, ul (underline), blink, and opposite (bandy foreground and background).

External Merge and Unequal Tools

Although Git has an internal implementation of unequal, which is what we've been showing in this book, you tin can set up an external tool instead. You tin can also set up a graphical merge-disharmonize-resolution tool instead of having to resolve conflicts manually. We'll demonstrate setting up the Perforce Visual Merge Tool (P4Merge) to practise your diffs and merge resolutions, considering it'due south a squeamish graphical tool and it'southward free.

If yous want to attempt this out, P4Merge works on all major platforms, so you lot should exist able to do so. We'll use path names in the examples that work on macOS and Linux systems; for Windows, y'all'll take to change /usr/local/bin to an executable path in your environment.

To begin, download P4Merge from Perforce. Next, you'll set up external wrapper scripts to run your commands. We'll use the macOS path for the executable; in other systems, it will be where your p4merge binary is installed. Gear up a merge wrapper script named extMerge that calls your binary with all the arguments provided:

              $ cat /usr/local/bin/extMerge #!/bin/sh /Applications/p4merge.app/Contents/MacOS/p4merge $*            

The diff wrapper checks to make certain seven arguments are provided and passes 2 of them to your merge script. By default, Git passes the post-obit arguments to the diff program:

              path old-file erstwhile-hex old-mode new-file new-hex new-mode            

Considering you but desire the old-file and new-file arguments, you utilise the wrapper script to laissez passer the ones you lot demand.

              $ true cat /usr/local/bin/extDiff #!/bin/sh [ $# -eq seven ] && /usr/local/bin/extMerge "$two" "$five"            

Y'all too need to make sure these tools are executable:

              $ sudo chmod +10 /usr/local/bin/extMerge $ sudo chmod +10 /usr/local/bin/extDiff            

At present you can ready up your config file to employ your custom merge resolution and unequal tools. This takes a number of custom settings: merge.tool to tell Git what strategy to utilize, mergetool.<tool>.cmd to specify how to run the control, mergetool.<tool>.trustExitCode to tell Git if the exit lawmaking of that program indicates a successful merge resolution or not, and unequal.external to tell Git what command to run for diffs. Then, you lot tin either run iv config commands:

              $ git config --global merge.tool extMerge $ git config --global mergetool.extMerge.cmd \   'extMerge "$Base" "$LOCAL" "$REMOTE" "$MERGED"' $ git config --global mergetool.extMerge.trustExitCode imitation $ git config --global diff.external extDiff            

or you can edit your ~/.gitconfig file to add these lines:

              [merge]   tool = extMerge [mergetool "extMerge"]   cmd = extMerge "$BASE" "$LOCAL" "$REMOTE" "$MERGED"   trustExitCode = false [unequal]   external = extDiff            

After all this is set, if you run diff commands such as this:

              $ git diff 32d1776b1^ 32d1776b1            

Instead of getting the diff output on the control line, Git fires upward P4Merge, which looks something like this:

P4Merge

Figure 142. P4Merge

If you try to merge ii branches and subsequently have merge conflicts, you can run the command git mergetool; it starts P4Merge to let yous resolve the conflicts through that GUI tool.

The prissy thing about this wrapper setup is that you lot can change your unequal and merge tools easily. For example, to change your extDiff and extMerge tools to run the KDiff3 tool instead, all you have to do is edit your extMerge file:

              $ true cat /usr/local/bin/extMerge #!/bin/sh /Applications/kdiff3.app/Contents/MacOS/kdiff3 $*            

Now, Git volition use the KDiff3 tool for diff viewing and merge conflict resolution.

Git comes preset to use a number of other merge-resolution tools without your having to set up the cmd configuration. To see a listing of the tools it supports, endeavour this:

              $ git mergetool --tool-help 'git mergetool --tool=<tool>' may exist fix to 1 of the following:         emerge         gvimdiff         gvimdiff2         opendiff         p4merge         vimdiff         vimdiff2  The post-obit tools are valid, just non currently available:         araxis         bc3         codecompare         deltawalker         diffmerge         lengthened         ecmerge         kdiff3         meld         tkdiff         tortoisemerge         xxdiff  Some of the tools listed above merely work in a windowed environment. If run in a terminal-only session, they will fail.            

If you lot're not interested in using KDiff3 for diff but rather desire to employ it but for merge resolution, and the kdiff3 command is in your path, and then you can run:

              $ git config --global merge.tool kdiff3            

If you run this instead of setting up the extMerge and extDiff files, Git volition use KDiff3 for merge resolution and the normal Git unequal tool for diffs.

Formatting and Whitespace

Formatting and whitespace issues are some of the more frustrating and subtle problems that many developers meet when collaborating, especially cross-platform. It's very easy for patches or other collaborated work to introduce subtle whitespace changes because editors silently introduce them, and if your files always touch a Windows system, their line endings might be replaced. Git has a few configuration options to help with these issues.

core.autocrlf

If you're programming on Windows and working with people who are not (or vice-versa), you'll probably run across line-ending bug at some point. This is because Windows uses both a carriage-render character and a linefeed character for newlines in its files, whereas macOS and Linux systems use but the linefeed character. This is a subtle but incredibly abrasive fact of cantankerous-platform piece of work; many editors on Windows silently supercede existing LF-manner line endings with CRLF, or insert both line-ending characters when the user hits the enter key.

Git tin can handle this past auto-converting CRLF line endings into LF when you add a file to the index, and vice versa when information technology checks out lawmaking onto your filesystem. You lot can plow on this functionality with the cadre.autocrlf setting. If you're on a Windows machine, set it to true — this converts LF endings into CRLF when you bank check out lawmaking:

                $ git config --global core.autocrlf truthful              

If yous're on a Linux or macOS arrangement that uses LF line endings, and then you lot don't want Git to automatically catechumen them when you cheque out files; however, if a file with CRLF endings accidentally gets introduced, and so you may want Git to fix information technology. You can tell Git to convert CRLF to LF on commit simply not the other way around by setting core.autocrlf to input:

                $ git config --global cadre.autocrlf input              

This setup should go out you with CRLF endings in Windows checkouts, but LF endings on macOS and Linux systems and in the repository.

If you lot're a Windows programmer doing a Windows-only project, then you lot can plough off this functionality, recording the carriage returns in the repository by setting the config value to false:

                $ git config --global core.autocrlf false              

core.whitespace

Git comes preset to discover and fix some whitespace issues. It can look for 6 primary whitespace bug — three are enabled by default and can be turned off, and three are disabled by default but can be activated.

The three that are turned on by default are blank-at-eol, which looks for spaces at the end of a line; bare-at-eof, which notices blank lines at the end of a file; and space-before-tab, which looks for spaces before tabs at the beginning of a line.

The three that are disabled by default but tin can be turned on are indent-with-non-tab, which looks for lines that begin with spaces instead of tabs (and is controlled by the tabwidth option); tab-in-indent, which watches for tabs in the indentation portion of a line; and cr-at-eol, which tells Git that carriage returns at the terminate of lines are OK.

You can tell Git which of these you want enabled by setting core.whitespace to the values you desire on or off, separated by commas. You can disable an choice by prepending a - in front of its name, or use the default value past leaving it out of the setting cord entirely. For example, if you want all but infinite-earlier-tab to exist fix, you can practice this (with trailing-space beingness a short-hand to comprehend both blank-at-eol and blank-at-eof):

                $ git config --global core.whitespace \     trailing-space,-space-before-tab,indent-with-non-tab,tab-in-indent,cr-at-eol              

Or you can specify the customizing role only:

                $ git config --global cadre.whitespace \     -space-before-tab,indent-with-non-tab,tab-in-indent,cr-at-eol              

Git volition detect these bug when yous run a git diff command and try to colour them so you tin can perchance set them earlier you commit. It will also apply these values to help you lot when you apply patches with git apply. When you're applying patches, y'all tin enquire Git to warn y'all if it's applying patches with the specified whitespace problems:

                $ git apply --whitespace=warn <patch>              

Or you can have Git try to automatically fix the issue earlier applying the patch:

                $ git apply --whitespace=gear up <patch>              

These options apply to the git rebase command also. If you've committed whitespace issues but haven't withal pushed upstream, you can run git rebase --whitespace=fix to have Git automatically fix whitespace bug as information technology'southward rewriting the patches.

Server Configuration

Not nearly as many configuration options are available for the server side of Git, but at that place are a few interesting ones yous may want to take note of.

receive.fsckObjects

Git is capable of making certain every object received during a push still matches its SHA-1 checksum and points to valid objects. Yet, information technology doesn't do this by default; it's a adequately expensive operation, and might slow down the operation, especially on large repositories or pushes. If you want Git to check object consistency on every push, you can strength it to do so by setting receive.fsckObjects to true:

                $ git config --system receive.fsckObjects true              

Now, Git will bank check the integrity of your repository before each button is accepted to make sure faulty (or malicious) clients aren't introducing corrupt information.

receive.denyNonFastForwards

If you rebase commits that you've already pushed and and so endeavor to push once more, or otherwise try to push a commit to a remote branch that doesn't comprise the commit that the remote branch currently points to, you'll be denied. This is generally good policy; merely in the case of the rebase, you may make up one's mind that y'all know what you're doing and can forcefulness-update the remote branch with a -f flag to your push command.

To tell Git to reject forcefulness-pushes, set receive.denyNonFastForwards:

                $ git config --arrangement receive.denyNonFastForwards true              

The other way you can do this is via server-side receive hooks, which we'll cover in a bit. That approach lets you do more complex things similar deny not-fast-frontwards to a certain subset of users.

receive.denyDeletes

One of the workarounds to the denyNonFastForwards policy is for the user to delete the branch then push information technology dorsum up with the new reference. To avoid this, set up receive.denyDeletes to true:

                $ git config --system receive.denyDeletes truthful              

This denies any deletion of branches or tags — no user can do information technology. To remove remote branches, you must remove the ref files from the server manually. In that location are also more interesting ways to practise this on a per-user basis via ACLs, equally you'll acquire in An Case Git-Enforced Policy.

Source: https://www.git-scm.com/book/en/v2/Customizing-Git-Git-Configuration

Posted by: myerslizeatied.blogspot.com

0 Response to "How To Configure Git Bash On Windows"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel