Install Git
Terminal
Check git
the 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 Git
installation program can Git
be 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 Git
commit 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 Git
the working directory, but you cannot submit them, such as configuration files that save database passwords, etc., which git status
will be displayed every time Untracked files ...
. In this case, you can ignore the special files .gitignore
to make it easier. Conveniently solve this problem.
Rules for ignoring files
In daily use, .gitignore
the 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:
- Ignore files automatically generated by the operating system, such as thumbnails, etc.;
- 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;
- 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 .gitignore
submit it to Git
and 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 .gitignore
ignored. If you really want to add the file, you can -f
force it to be added Git
:
git add -f app.swift
Check ignore rules
When adding a file (such as app.swift) Git
fails, you guess there may .gitignore
be something wrong with the writing. You can check it.
git check-ignore -v app.swift
<!-- 输出 .gitignore:3:*.swift app.swift -->
Git
will tell us .gitignore
that 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
Git
There is also a way for everyone to Git
be lazy when typing commands - that is to configure Git
aliases.
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
Git
configuration files are usually stored in warehouse.git/config
files (hidden files in the root directory of your project) - The current user's
Git
configuration file is placed in a hidden file in the user's home directory.gitconfig
(you cancd
go to the home directory in the user's home directory on your computer)
view configuration file
cat .gitconfig
open configuration file
open .gitconfig