Git basic manual

1 Basic configuration

1. CentOS7 generally installs git by default, which can be viewed by the following command:

$ rpm -qa git
git-1.8.3.1-13.el7.x86_64

2. If it is not installed, you can use yum to install:

$ yum install git -y

3. The use of git configuration files are as follows, which can be viewed by the following command:

$ git config
usage: git config [options]

Config file location
    --global              使用全局配置文件
    --system              使用系统级配置文件
    --local               使用版本库级配置文件
    -f, --file <file>     使用给定的配置文件

4. Configure user mailbox syntax highlighting using git:

$ git config --global user.name 'leon'
$ git config --global user.email '[email protected]'
$ git config --global color.ui true

5. View the configuration:

$ git config --list
user.name=leon
[email protected]
color.ui=true

$ cat .gitconfig 
[user]
        name = leon
        email = [email protected]
[color]
        ui = true

2 git initialization

1. Initialization:

$ mkdir /gitdir
$ cd /gitdir
$ git init
Initialized empty Git repository in /gitdir/.git/

2. Check the status:

$ git status
# On branch master
#
# Initial commit
#
nothing to commit (create/copy files and use "git add" to track)

3. After the initialization is completed, a hidden folder named .git will be generated in the working directory.

$ ls .git/ | xargs -n1
branches	# 分支目录
config		# 定义项目的特有配置
description	# 描述
HEAD		# 当前分支
hooks		# git 钩子文件
info		# 包含一个全局排除文件(exclude)
objects		# 存放所有数据,包含 info 和 pack 两个子文件夹
refs		# 存放指向数据(分支)的提交对象的指针
index		# 保存暂存区信息

3 Basic commands

1. Create a test file in the working directory:

$ touch a b c

2. Submit documents to the temporary storage area:

# 提交指定文件
$ git add a
# 提交所有文件,也可使用 * 代替 .
$ git add .

3. Withdraw documents submitted to the temporary storage area:

# 撤回指定文件
$ git rm --cached a
rm 'a'

4. Delete the file submitted to the temporary storage area:

# 先从暂存区撤回到工作区,然后直接删除文件
$ git rm --cached c
$ rm -f c
# 直接同时删除工作目录和暂存区的文件
$ git rm -f b

5. Submit all files in the current temporary storage area to the local warehouse:

$ git commit -m 'commit a'   

6. Rename the file that has been submitted to the local warehouse:

# 手动修改
$ mv a a.txt
$ git rm --cached a
$ git add a.txt
$ git commit -m 'rename a to a.txt'
# git 修改,把工作区和暂存区的文件同时修改
$ git mv a.txt a
$ git commit -m 'rename a.txt to a'

7. Compare the similarities and differences between the files in the working directory and the files in the temporary storage area:

$ echo 111 > a
# 工作目录的文件和暂存区文件内容不同了
$ git diff

8. Compare the similarities and differences between the contents of the files in the temporary storage area and the local warehouse:

# 此时暂存区和本地仓库内容相同
$ git diff --cached
# 提交到暂存区
$ git add a
# 此时暂存区和本地仓库内容不同了
$ git diff --cached

9. At the same time, add the file to the temporary storage area and submit it to the local warehouse:

$ git commit -am "add newfile"

10. View the current version and previous submission logs:

# 多行显示详细信息
$ git log
# 一行显示简要信息
$ git log --oneline
# 显示当前所在分支及版本
$ git log --oneline --decorate
# 显示提交的具体内容
$ git log -p
# 显示最近提交的 2 条内容
$ git log -2
# 查看指定提交记录的详细内容
$ git log 6ef6282 -p

11. Roll back to the specified version:

# 这里 744a169 是通过 git log --oneline 查看到的版本号
$ git reset --hard 744a169 

12. View the submission logs of all versions (including after the current version):

$ git reflog

4 branches

1. Create a branch:

$ git branch testing

2. View all branches and the current branch:

$ git branch

3. Switch to the specified branch:

$ git checkout testing 

4. Merge the specified branch to the current branch:

$ git merge testing

5. Delete the specified branch:

$ git branch -D testing

6. Create and switch to the designated branch at the same time:

$ git checkout -b testing

5 tags

1. Create a label:

# 给当前版本打标签
$ git tag -a v1.0 -m 'init v1.0'
# 给指定版本打标签
$ git tag -a v0.4 718c699 -m 'init v0.4'

2. View the label:

# 查看所有已打标签
$ git tag
# 查看指定标签详细信息
$ git show v1.0 

3. Roll back to the specified label:

$ git reset --hard v0.4

6 github remote warehouse

1. Create a warehouse on github, copy the ssh link of the warehouse, and execute the following command to add a remote warehouse:

$ git remote add origin [email protected]:pingface/git_data.git

2. If the addition is successful, you can view the added remote warehouse by the following command:

$ git remote

3. To set up secret-free push, you need to add the public key to the remote warehouse:

$ ssh-keygen 
# 将公钥内容复制到 github 的 Settings -> SSH AND GPG keys 页中 SSH keys 下
$ cat .ssh/id_rsa.pub 

4. Push the current master branch to the remote warehouse:

$ git push -u origin master

5. Download the code of the remote warehouse:

$ git clone [email protected]:pingface/git_data.git

Guess you like

Origin www.cnblogs.com/centlnx/p/12735305.html