Personal git notes, continuous learning and supplementary filling

git --version //查看git版本信息
sudo yum remove git -y //卸载git

sudo yum install git -y//安装git

This article is only for the convenience of personal daily viewing, and some places have not been introduced in detail

git init

Create a local warehouse (it is best to create a directory first, enter the command to create a git warehouse in the directory), after the creation is completed, there will be a .git file in the directory

warehouse configuration

git config user.name "xxxxx"      //设置用户名称
git config user.email "[email protected]"  //设置email地址
git config -l                     //查看当前仓库配置

git config --unset user.name      //删除用户名
git config --unset user.email     //删除email

git config --global color.ur true  //打开git的默认颜色配置
git config --global color.ur false //关闭颜色配置

You can add the --global option when configuring, and add the global attribute after adding it. All warehouses under this machine use this setting, and add --global when deleting the global attribute

//配置全局信息
git config --global user.name "xxx"
git config --global user.email "xxxx"

//删除全局配置
git config --global --unset user.name
git config --global --unset user.email

warehouse information

Workspace: All files except .git files in the warehouse directory are in the workspace

Temporary storage area: After git add filename is performed on the files in the workspace, the filename in the workspace will be submitted to the temporary storage area, corresponding to the index under the .git file.

Repository: After entering commit -m "xxxxxx", the contents of the temporary storage area will be submitted to the repository. At this time, there will be an objects in the .git directory, and the contents of this directory are commit objects one by one.

 view information

git log                   //查看commit信息
git log --pretty=oneline  //更加优雅的显示信息

 What git tracks and manages is modification (addition, deletion, check and modification).

git status                //查看仓库状态,上一次提交到现在是否发生修改
git diff filename         //查看暂存区和工作区filename文件的差异
git diff HEAD -- filename //查看版本库和工作区filename文件的差异

git cat-file -p id        //查看commit id索引的内容


//工作区的内容修改完后,如果想提交到版本库要进行add commit相关操作 
git add filename       //将filename添加到暂存区
git commit -m "xxx"    //将暂存区的内容提交到版本库

version rollback

git reset --soft xxxxx    //只回退版本库的内容,回退到指定commit的id索引处
git reset --mixed xxxx    //回退版本库和暂存区的内容,回退到指定id,该选项为默认选项
git reset --hard xxxx     //工作区、暂存区、版本库的内容都进行回退,慎用

//回退后git log也随之回退

git reflog                //记录本地每一次的提交记录,可以根据里查看id,帮助回退

undo operation

//只撤回工作区的内容
git checkout -- filename //撤销当前工作区的filename的内容,保持与版本库一致(当修改代码,又后悔了后想回到修改之前时使用)

//撤回工作区和暂存区的内容(即修改文件后,只进行了add)
git reset --mixed HEAD    //HEAD:当前版本库的内容,这里先将暂存区回退到当前版本库的内容
                          //HEAD^:上一个版本库
                          //HEAD^^:上两个版本库的内容...
git checkout -- filename  // 然后再撤回工作区的内容

//撤回工作区、暂存区、版本库的内容
git reset --hard xxx(HEAD版本或者其它版本)      //使用--hard即可实现

delete repository files

1. The rm command deletes the file, then git add the file name, and then git commit -m "xxxx"

2. git rm filename, then git commit -m "xxxxx"

git branch management

create/switch/delete branch

The HEAD pointer in .git points to the master branch, and the most recent commit index is stored in the master.

git branch            //查看当前库的分支   *表示当前所在分支
git branch xxx        //创建xxx分支
git checkout xxx      //切换到xxx分支(即修改HEAD指向,xxx分支指向最新一次commit)
git merge xxx         //将xxx分支合并
//利用分支操作比直接使用master分支更加安全

git branch -d xxx     //删除xxx分支

branch conflict

git checkout -b xxx //新建xxx分支,并切换到该分支

Scenes:

There is a file readme in the current master, and its version library content is: aaa

At this point, we create and switch to a new branch dev, modify the content of the readme in the workspace under this branch, add a new line of bbb, and submit it to the repository.

At this time, switch back to the master branch, then modify the readme under the master, add a new line of ccc, and then submit it to the repository. If you merge at this point, an error will occur.

git log --graph --abbrev-commit   //图形化形式显示分支信息

Branch switching is actually changing the pointing of HEAD, and modifying the content of the branch to the commit id of the latest commit. So very efficient.


Guess you like

Origin blog.csdn.net/qq_60192898/article/details/131403316