macOS: Install and configure Git

Install Git

TerminalCheck gitthe version in the terminal

git --version

If you have not installed the command line developer tools, you will be prompted to install them.

If you want to install a newer version, you can use the binary installer. The officially maintained macOS Gitinstallation program can Gitbe downloaded from the official website at: https://git-scm.com/download/mac .

Git environment configuration

Configure username and email address

After installation Git, the first thing to do is to set up your username and email address. This is important because every Gitcommit uses this information.

git config --global user.name "吴老师"
git config --global user.email [email protected]

Check configuration

You can view all configurations and the files in which they are located with the following command:

git config --list --show-origin

Git color configuration

Let Git display colors to make command output look more eye-catching

git config --global color.ui true

Git ignores file configuration

Sometimes, you must put certain files into Gitthe working directory, but you cannot submit them, such as configuration files that save database passwords, etc., which git statuswill be displayed every time Untracked files .... In this case, you can ignore the special files .gitignoreto make it easier. Conveniently solve this problem.

Rules for ignoring files

In daily use, .gitignorethe file already has various ready-made configuration files, which only need to be combined to use. All configuration files can be browsed directly online: https://gitcode.net/codechina/gitignore

The rules for ignoring files are:

  1. Ignore files automatically generated by the operating system, such as thumbnails, etc.;
  2. Ignore intermediate files, executable files, etc. generated by compilation. That is, if a file is automatically generated through another file, then there is no need to put the automatically generated file into the repository, such as the .class file generated by Java compilation;
  3. Ignore your own configuration files that contain sensitive information, such as passwords.
    For example: ignore the .pyc, .pyo, dist and other files or directories generated by compilation
    *.py[cod]
    *.so
    *.egg
    *.egg-info
    dist
    build
    

The last step is to .gitignoresubmit it to Gitand you're done!

Force adding ignored files

Sometimes, you want to add a file (such as app.swift) Git, but find that it cannot be added because the file is .gitignoreignored. If you really want to add the file, you can -fforce it to be added Git:

git add -f app.swift

Check ignore rules

When adding a file (such as app.swift) Gitfails, you guess there may .gitignorebe something wrong with the writing. You can check it.

git check-ignore -v app.swift
<!-- 输出 .gitignore:3:*.swift app.swift -->

Gitwill tell us .gitignorethat line 3 of the rule is ignoring the file, so we can know which rule should be revised.

Add exception rules

There are also times when we write rules to exclude some files:

# 排除所有.开头的隐藏文件:
.*
# 排除所有.class文件:
*.class

But we found that the .* rule also excludes .gitignore. At this time, although you can use git add -f to force it in, it is recommended that you add an exception rule:

# 排除所有.开头的隐藏文件:
.*
# 排除所有.class文件:
*.class

# 不排除.gitignore:
!.gitignore

Git configuration alias

GitThere is also a way for everyone to Gitbe lazy when typing commands - that is to configure Gitaliases.
We only need to type a line of command to tell Git that st will represent status from now on:

git config --global alias.st status

Of course there are other commands that can be abbreviated:

git config --global alias.co checkout
git config --global alias.ci commit
git config --global alias.br branch

After configuring the above alias, future submissions can be abbreviated as:

git ci -m "frist commit"

Configurationgit reset HEAD file

git config --global alias.unstage 'reset HEAD'

configure git log-1

View the last commit information:

git config --global alias.last 'log -1'

Configurationgit lg

git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"

Git configuration file

  • These customized Gitconfiguration files are usually stored in warehouse .git/configfiles (hidden files in the root directory of your project)
  • The current user's Gitconfiguration file is placed in a hidden file in the user's home directory .gitconfig(you can cd go to the home directory in the user's home directory on your computer)

view configuration file

cat .gitconfig

open configuration file

open .gitconfig

Guess you like

Origin blog.csdn.net/wujakf/article/details/128532032