Basic common commands of GIT

Local initialization:

If you want to use Git to manage a project, you must initialize Git init, which generates a hidden file in the current directory.

git init # 创建新仓库

git config --global user.email "[email protected]" # 设置全局email

git config --global user.name "Anderson" # 设置全局用户名

git config --global user.email "" # 清空配置的email

git config --global user.name ""  # 清空配置的全局名

.gitThe folder saves all information related to the local warehouse git. If you want to restore it to a normal folder:

rm -rf .git  # 删除.git这个隐藏文件即可

Clone a local repository from a remote Git repository:

git clone  # 后面加上Git仓库的地址

Workflow:

The local warehouse is composed of 3 areas. It holds your source code and other actual files. It is like a buffer between the workspace and the repository. It temporarily stores your changes, records each of your submissions, and maintains several branches.工作区暂存区本地版本库

View file status

All files are in one of two states: untracked  and  tracked .

Untracked  files are not included in git version control.

The tracked state can be subdivided into the following three states:

     ① unmodified: consistent with the latest file in the repository

     ② modified: just modified

     ③ staged: modified and added to the temporary storage area , waiting for submission

Workspace : The place where we write code and the working directory is called the workspace.

Temporary storage area :

              ① Temporary storage area. In git, the code cannot be submitted directly from the work area to the warehouse area.

              ② If you want to submit the code from the work area to the warehouse area, you must first submit it to the staging area and then submit it to the warehouse area.

              ③The purpose of the temporary storage area is to avoid misoperation.

Warehouse area :

             ① Permanently transfer the content saved in the temporary storage area to the git warehouse and generate a version number.

             ② Based on the generated version number, we can roll back to any version in history.

git status  # 查看文件状态

# 红色的表示工作区的文件需要提交
# 绿色的表示暂存区中的文件需要提交
git add index.vue  # 将index.vue单个文件添加到暂存区

git add css  # 将css目录下的所有文件添加到暂存区

git add all 
gti add .   # 这两个都表示,将所有的文件都添加到暂存区

As long as the files in the workspace are modified, the modified files must be added to the temporary storage area.

git commit -m "提交的说明文字"  # 将文件从暂存区提交到仓库,这将会生成版本号存档

Note: If no description is written, the submission will be unsuccessful.

Pull the remote code and update the local code

git pull # 拉取代码

Push local code to remote repository

git push # 推送到远程

// 把刚创建的分支推送到远程
git push --set-upstream origin 刚创建的分支名

List all local branches: the current branch will have an * sign in front of it

git branch  # 查看所有的分支

switch branch

git checkout 分支名  # 切换分支

Create a remote branch

git push origin 分支名  # 把刚创建的本地分支,创建一个远程分支

Create a branch

git checkout -b 分支名 # 创建本地分支并切换分支
// 这条命令把创建本地分支和切换到该分支的功能结合起来了

git branch 分支名 # 基于当前分支创建一个本地分支
// 这条命令是创建一个新分支出来,剩下的操作还要切换到新分支、再把新分支与远程分支关联。

Associate local branch with remote branch

git branch –set-upstream 本地分支名 origin/远程分支名 
或者
git branch –set-upstream-to=origin/远程分支名

// 示例:git branch –set-upstream-to=origin/test  # 把本地test分支与远程进行关联

delete branch

git branch -d 分支名称  # 删除分支

// 不能在当前分支删除当前分支,需要切换到其他分支才能删除。
// master 分支一般是主分支,不建议删除。

Rename branch locally

git branch -m 原始名称 新名称  # 本地分支重命名
 
//示例: 修改 test 为 newTest
// git branch -m test  newTest

Remote branch duplicate name

git branch -m 旧分支名称  新分支名称  # 先重名名本地分支

git push --delete origin 旧分支名称  # 删除远程分支

git push origin 新分支名称  # 把本地分支推到远程,这时候系统会自动关联分支

merge branches

git merge 分支名 # 合并分支
// 在当前分支合并其他分支

If there is a merge conflict, you need to manually resolve the conflict or roll back the code; after resolving the conflict, you need to git add again.

View the commit history of a repository

git log  # 查看历史记录,这个看不了已回退的版本记录

git reflog  # 查看所有的版本信息,包括已回退的版本

Rolling back the version, rolling back the code to a historical version, requires the historical version number

git reset --hard 版本号  # 将代码回退到历史的某一个版本,需要版本号

Roll back the code in the staging area, after git add

git reset HEAD # 后面什么都不加,就是回退全部的

git reset HEAD 文件名  # 回退单个文件名

Roll back the code in the warehouse area, after git commit

git reset --soft HEAD^ # 回退commit的提交,仅仅是撤回commit操作,但写的代码仍然保留
// HEAD^意思是上一个版本,
// 也可以写成HEAD~1,如果你进行了2次commit,想都撤回,可以使用HEAD~2。


git reset --mixed HEAD^ 
// 不删除工作空间改动代码,撤销commit,并且撤销git add . 操作

git reset --soft HEAD^
// 不删除工作空间改动代码,撤销commit,不撤销git add .

git reset --hard HEAD^ 
//  删除工作空间改动代码,撤销commit,撤销git add .

Modify commit comments

git commit -m 新注释 --amend  
// git commit 加上 --amend 参数,并用 -m 指明新的注释,执行后便会覆盖掉老的注释

Git review rollback

git checkout 分支名  # 切换到需要回退的分支
git log  # 查看提交记录
git reset --hard # 代码回退
git reset --hard id # id为commit提交的记录
git review  # 重新提交修改

Git rebase rollback

git reflog  # 先查看本地提交操作编号
// 找到提交前的项目编号,例如4c173eb HEAD@{3}: commit: scan ,
// 执行:git reset --hard 4c173eb

Git cherry-pick fallback

git cherry-pick --abort

Guess you like

Origin blog.csdn.net/weixin_48674314/article/details/129141635