Home> Development Tools> git> body text

What are the commonly used operating commands in git? Summary of common operation commands

青灯夜游
Release: 2019-01-05 10:14:08
forward
5191 people have browsed it

What are the commonly used operating commands in git? This article summarizes some commonly used operating commands in git. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

git start

Global configuration: configure user name and e-mail address

$ git config --global user.name"Your Name" $ git config --global user.email"email@example.com"
Copy after login

git init: Initialize the git warehouse and generate a .git file in the directory

git init
Copy after login

git add File name:Modify and add the file To the warehouse

git add readme.txt //修改单个文件
Copy after login
git add . // 将所有修改的文件添加到暂存区
Copy after login

git commit -m 'Instructions': Submit the file to the warehouse

$ git commit -m "wrote a readme file"
Copy after login

git status: View the status of the current warehouse, Master the status of the workspace

git diff readme.txt: View the modified content of the file

Version rollback

git log: View history

git log --pretty=oneline: Only view commit.id (version number) and description

git reset --hard HEAD^: Roll back to the previous version HEAD represents the current version, the previous one is HEAD^, and the next one hundred is HEAD~100

git reset --hard commit.id: Return to the version corresponding to the specified version number

git reset --hard 1049a
Copy after login

git reflog: used to record each of your commands for determination Which version to return to

A few concepts

Working Directory: Perform git on the computer Operating directory

Repository: The .git file in the workspace is the repository. The most important thing in the git repository is called stage (or index) In the staging area, there is also the first branchmasterthat Git automatically created for us, and a pointer tomastercalledHEAD.

When adding a file to the Git repository, it is executed in two steps:

The first step is to usegit addAdd the file, which actually adds the file modification to the temporary storage area;

The second step is to usegit committo submit the changes, In fact, all the contents of the staging area are submitted to the current branch.

Undo modifications

Undoing is divided into three situations:

The first isreadme.txtIt has not been placed in the temporary storage area since the modification. Now, undoing the modification will return to the same state as the repository; (no add) ---> git checkout -- file

The second is that afterreadme.txthas been added to the temporary storage area, it has been modified. Now, undo the modification and return to the addition. The state after reaching the staging area. (no commit) ---> git reset head file

The third way is that readme.txt has been committed, just use version rollback ---> git reset --hard head^

1. git checkout -- readme.txt: Undo all modifications to the specified file in the workspace

##Note: The--in the command is very important. Without--, it becomes the "switch to another branch" command

2. git reset: You can either roll back the version or roll back the modifications in the temporary storage area to the workspace

In the second case, we You can use the git reset command to withdraw the workspace

git reset head readme.txt // head表示当前版本
Copy after login

After withdrawing the workspace, use the git checkout command to withdraw from the workspace

Delete the file

git rm file: Delete the file

from the repository. If you delete it accidentally, you can use the version of the repository. 'one click recovery'

git checkout -- test.txt
Copy after login

远程仓库

git remote add origin github仓库地址:将本地仓库与远程仓库关联

git push -u origin master:由于远程库是空的,我们第一次推送master分支时,加上了-u参数,Git不但会把本地的master分支内容推送的远程新的master分支,还会把本地的master分支和远程的master分支关联起来,在以后的推送或者拉取时就可以简化命令。

git push origin master: 将本地master分支的修改推送到远程仓库

git clone github仓库地址:将远程仓库克隆到本地

分支管理

git branch dev:创建dev分支

git checkout dev :切换dev分支

git checkout -b dev:创建dev分支,并切换到dev分支。 -b参数表示创建并切换到dev分支

git branch: 查看所有分支, *表示当前分支

git merge dev: 合并指定分支到当前分支 。 结果中Fast-forward信息表示‘快进模式’ -->直接把master指向dev的当前提交,合并速度非常快

git merge --no-ff -m '描述内容' dev :--no-ff参数表示禁用Fast-forward,使用普通模式

git branch -d dev: 删除dev分支

git branch -D dev: 如果分支还没合并,使用-d无法删除,使用-D强制删除

git log --graph: 可以查看分支合并图。

git log --graph --pretty=oneline --abbrev-commit :--pertty=oneline 查看简短信息 --abbrev-commit: 查看commit缩写

场景:修复bug时创建分支

git stash: 将当前工作现场存储起来

git stash list :查看存储的工作现场列表

git stash pop:恢复工作现场并删除stash的内容

git remote: 查看远程仓库的信息

git remote -v: 查看origin的地址

git checkout -b dev origin/dev:在本地创建和远程分支对应的分支

git pull:抓取最新的远程提交

git branch --set-upstream-to=origin/dev dev: 建立本地分支和远程分支的关联

git rebse: 把本地未push的分叉提交历史整理成直线

标签管理

git tag : 用于创建一个新标签,默认指向head,也可以指向commi.id

git tag: 用于查看所有标签

git tag -a -m '标签信息' :为标签指定信息

git tag -d : 删除本地标签

git push origin :推送本地标签到远程

git push origin --tags: 推送全部未推送的本地标签到远程

git push origin :refs/tags/ :删除一个远程标签

自定义git

git config --global alias.'自定义简写指令' '被简写的指令'

git config --global alias.st statusgit config --global alias.co checkout
Copy after login

命令行命令

cat readme.txt: 查看文件内容

rm file 删除文件

vi file: linux里的vi编辑器

(1)通过i键进入插入模式,可以修改文件

(2)通过Esc键进入命令模式 输入':wq!' -->保存+退出vi 输入':q!' -->不保存退出

The above is the detailed content of What are the commonly used operating commands in git? Summary of common operation commands. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
git
source:cnblogs.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!