Git version control tool - commonly used commands (2)

local command

1. Download the program from the git official website and install it by default.
Reference Git version control tool - background introduction (1)

2. Set up the association between account and email. The account and email can be accounts of Code Cloud, GitLab, etc.:

$ git config --global user.name "Your Name"             
$ git config --global user.email "[email protected]"

3. Select the appropriate place and create an empty directory:

$ mkdir test        //创建空目录,目录名字为test
$ cd test        //进入test目录

4. Initialize the warehouse and change the directory into a warehouse that can be managed by git:

$ git init
$ ls         //查看文件
$ ls -ah       //如果.git目录是影藏的话,可以通过这个命令查看.git目录

5. Submit the file to git

$ git add .                  //告诉Git,把文件添加到仓库,此时是将修改添加到暂存区,可add 多次
$ git commit -m '本次提交的备注'       //告诉Git把文件提交到仓库,此时是吧暂存区的所有内容提交到当前分支,可一次提交很多文件

6. Check the status of the current warehouse

$ git status    //查看版本库状态,什么被修改过但还没提交的

$ git diff      //查看当前相对上一次提交修改的内容

7. Version rollback

$ git log                         //显示从最近到最远的提交日志
$ git log   --pretty== oneline     //显示log,但是不显示很多凌乱的信息
q                                //显示log版本信息有很多,使用q键停止查看
git reset —hard head^         //回退到上一个版本
git reset —hard head^^        //回退到上上个版本
git reset —hard head~100      //回退到之前100个版本
git reset —hard +commit_id    //回到某个版本号的版本

git reset — hard 版本号     //版本回退多次后需要恢复最新版本

$ git reflog                     //查看曾经使用过的命令

8. Undo modifications

$ git checkout -- test.html

9. Delete files

$ rm test.index     //可直接在文件管理中删除文件,要不用rm 命令去删除

$ git rm test.html    //从版本库中删除
$ git commit -m '删除 test.html文件'
$ git branch -D <name>     //丢弃一个没有被合并过的分支,可以通过强行删除。

10. Clone

$ git clone 需要克隆的仓库地址

11. Create a branch and switch to it

$ git checkout -b 新分支的名字       //创建分支并且切入进分支

或者等同于

$ git branch 分支名       //创建分支
$ git checkout 分支名     //切换到分支
$ git branch               //查看分支

12. Merge branches

$ git checkout -b dev
$ git branch
$ git add .
$ git commit -m '提交test文件到dev分支'
$ git checkout master     //切换到主分支
$ git merge dev        //将dev分支上的内容合并到master分支上,合并 指定分支 到 当前分支
$ git merge --no-ff -m "merge with no-ff" dev  //合并分支时加上--no-ff参数就可以用普通模式合并,合并后的历史有分支,通过git log查看
$ git branch -d dev     //删除dev分支

13. Conflict resolution:
If there is a conflict in modifying the same file, you need to manually resolve the conflict before submitting it. You can view conflicts with git status, modify the conflicting parts according to the marks, and then resubmit after the modification is completed.

$ git pull         //拉取远程内容
$ git log --graph        //命令可以看到分支合并图。

14. Associate local warehouse and remote warehouse

$ git branch --set-upstream-to <branch-name> origin/<branch-name>

15. Create tags

$ git branch 
$ git checkout dev
$ git tag v1.0      //为当前需要打标签的分支打新标签

$ git tag        //查看所有标签
$ git tag -a 指定标签信息 -m "blablabla..."   //可指定标签信息 

16. Operation label

$ git push origin <tagname>     //可以推送一个本地标签;
$ git push origin --tags        //可以推送全部未推送过的本地标签;
$ git tag -d <tagname>        //可以删除一个本地标签;
$ git push origin :refs/tags/<tagname>     //可以删除一个远程标签。

Remote warehouse

1. Create SSH Key (two files, id_rsa private key and id_rsa.pub public key, need to be generated)

$ ssh-keygen -t rsa -C "[email protected]"

2. Log in to GitHub, set "SSH Keys", and copy the content of id_rsa.pub to add it. Allows adding multiple SSHs.

3. Associated with remote warehouse

$ git remote add origin [email protected]:账户名

4. Push local content to the remote library branch

$ git push -u origin 分支名字       //第一次推送分支所有内容
$ git push origin 分支名字          //推送最新修改

5. View remote warehouse information

$ git remote 

$ git remote -v      //查看更加详细的信息

6. Change Git color

$ git config --global color.ui true

reference:

Summary of common git commands

Guess you like

Origin blog.csdn.net/qq_42886163/article/details/102710487