# Configure the user which will be used by git
Of course you should use your name
git config --global user.name “Example Surname”
Same for the email address
git config --global user.email “your.email@gmail.com”
Set default so that all changes are always pushed to the repository
git config --global push.default “matching”
获取Git配置信息,执行以下命令:
git config --list
3.2. 高亮显示
以下命令会为终端配置高亮
git config --global color.status auto
git config --global color.branch auto
3.3. 忽略特定的文件
可以配置Git忽略特定的文件或者是文件夹。这些配置都放在.gitignore文件中。这个文件可以存在于不同的文件夹中,可以包含不同的文件匹配模式。为了让Git忽略bin文件夹,在主目录下放置.gitignore文件,其中内容为bin。
同时Git也提供了全局的配置,core.excludesfile。
3.4. 使用.gitkeep来追踪空的文件夹
Git会忽略空的文件夹。如果你想版本控制包括空文件夹,根据惯例会在空文件夹下放置.gitkeep文件。其实对文件名没有特定的要求。一旦一个空文件夹下有文件后,这个文件夹就会在版本控制范围内。
后续将通过一个典型的Git工作流来学习。在这个过程中,你会创建一些文件、创建一个本地的Git仓库、提交你的文件到这个仓库中。这之后,你会克隆一个仓库、在仓库之间通过pull和push操作来交换代码的修改。注释(以#开头)解释了命令的具体含义
让我们打开命令行开始操作吧
4.1. 创建内容
下面创建一些文件,它们会被放到版本控制之中
#Switch to home
cd ~/
Create a directory
mkdir ~/repo01
Switch into it
cd repo01
Create a new directory
mkdir datafiles
Create a few files
touch test01
touch test02
touch test03
touch datafiles/data.txt
Put a little text into the first file
ls >test01
4.2. 创建仓库、添加文件和提交更改
每个Git仓库都是放置在.git文件夹下.这个目录包含了仓库的所有历史记录,.git/config文件包含了仓库的本地配置。
以下将会创建一个Git仓库,添加文件倒仓库的索引中,提交更改。
# Initialize the local Git repository
git init
Add all (files and directories) to the Git repository
git add .
Make a commit of your file to the local repository
git commit -m “Initial commit”
Show the log file
git log
4.3. diff命令与commit更改
通过git diff命令,用户可以查看更改。通过改变一个文件的内容,看看git diff命令输出什么,然后提交这个更改到仓库中
# Make some changes to the file
echo “This is a change” > test01
echo “and this is another change” > test02
Check the changes via the diff command
git diff
Commit the changes, -a will commit changes for modified files
but will not add automatically new files
git commit -a -m “These are new changes”
4.4. Status, Diff 和 Commit Log
下面会向你展示仓库现有的状态以及过往的提交历史
# Make some changes in the file
echo “This is a new change” > test01
echo “and this is another new change” > test02
See the current status of your repository
(which files are changed / new / deleted)
git status
Show the differences between the uncommitted files
and the last commit in the current branch
git diff
Add the changes to the index and commit
git add . && git commit -m “More chaanges - typo in the commit message”
Show the history of commits in the current branch
git log
This starts a nice graphical view of the changes
gitk --all