[Git] Git common operation instructions

Git common operation instructions

1. Configure git

配置提交人姓名: git config --global user.name committer name

配置提交人姓名: git config --global user.email submitter email

查看git配置信息:git config --list

2. Create a repository

创建新的文件夹:mkdir xxx

创建新的文件:touch xxx

初始化git仓库:git init

把文件添加到暂存区: git add file (modified files that need to be submitted are all placed in the staging area)

把工作时的所有变化提交到暂存区:git add .

把文件提交到仓库: git commit -m "commit information" (indicating the meaning of this commit) (whenever you feel that the file has been modified to a certain extent, you can save it with commit. Once you change the file, or delete the file by mistake, You can also resume from the most recent commit and continue working.)

There are many things in the Git repository, the most important of which is the staging area called stage, the first branch master that Git automatically created for us, and a pointer to master called HEAD.

把文件往Git版本库里添加的时候,是分两步执行的

The first step is to use git add to add the file, which is actually to add the file modification to the staging area;

The second step is to commit the changes with git commit, which actually commits all the contents of the staging area to the current branch. Every time you modify, if you don't use git add to the staging area, it will not be added to the commit

insert image description here

3. Version rollback

查看仓库状态:git status

当对文件修改后,查看具体修改的内容:git diff

显示提交记录,显示版本号和提交人信息:git log (enter q to end)

版本回退: git reset --hard commit_id (commit_id is the version number of the rollback version, just enter the first few digits of the version number, such as 33bf43)

insert image description here

显示自己提交命令历史:git reflog

当你改乱了或删除工作区某个文件的内容,想直接撤销工作区的修改时:git checkout – xxx

当你不但改乱或删除工作区某个文件的内容,还添加到了暂存区时,想撤销修改,分两步: First git reset HEAD xxx can undo the modification of the temporary storage area and put it back in the workspace after git checkout – xxx undo the modification of the workspace

在工作区删除文件:rm xxx

在暂存区中删除文件:git rm xxx

4. Remote warehouse

Create a new repository on github learngit

在本地仓库关联到远程仓库:git remote add

比如:git remote add origin [email protected]:Better-XKF/learngit.git]

origin is an alias for a remote warehouse in the local warehouse, or it can be anything else. When connecting to github, you can write github, and when you connect to gitee, you can write gitee. In this way, our local library can synchronize with multiple remote libraries at the same time. If you want to push to GitHub, use the command git push github master, if you want to push to Gitee, use the command: git push gitee master

[email protected]: Better-XKF/learngit.git is the URL address of my github remote warehouse learngit

第一次把本地库的内容推送到远程库上: git push -u origin master (no need to add u later)

Since the remote library is empty, when we push the master branch for the first time, we add the -u parameter (it will not be modified later), Git will not only push the contents of the local master branch to the new remote master branch, but also push the local master branch to the new remote master branch. The master branch is associated with the remote master branch, which simplifies commands in future pushes or pulls.

查看远程库信息:git remote -v

删除远程库:git remote rm xxx

The "delete" here is actually to release the binding relationship between the local and the remote, not to physically delete the remote library. No changes have been made to the remote library itself. To actually delete the remote library, you need to log in to GitHub, find the delete button on the background page, and delete it.

克隆仓库:git clone

5. Branch management

For ease of understanding, a branch can be thought of as a copy of the code in the current working directory. Using branches allows us to separate from the main development line so as not to affect the main development line. Multiple things go on at the same time without affecting each other.

创建分支:git branch xxx

切换分支: git switch/checkout xxx (using switch is easier to understand than checkout)

创建并切换分支:git switch -c xxx/ git checkout -d xxx

查看当前所在的分支: git branch (list all branches, the current branch will be marked with a *.)

在分支xxx开发完,将代码改动的地方提交到仓库 ,xxx分支工作完成后切换到master分支:git switch master

It is found on the master branch that the changes in the xxx branch are not on the master branch. This is because the commit is on the xxx branch, and the commit point of the master branch has not changed at the moment.

合并分支: git merge xxx (merge xxx branch to current branch)

删除分支:git branch -d xxx

解决合并分支冲突问题: When Git can't automatically merge branches, it has to resolve conflicts first. After the conflict is resolved, commit again, and the merge is complete. Conflict resolution is to manually edit the files that failed to be merged by Git to the content we want, and then submit them. Use the git log --graph command to see the branch merge graph.

修复bug: When you get a task to fix a bug with codename 2, it is natural that you want to create a branch bug-2 to fix it, but the work currently being done on dev has not been committed

将工作现场进行保存:git stash

Switch to the master branch to create the bug-02 branch to fix the bug. After the fix is ​​complete, switch to the master branch and complete the merge. Then back to the dev branch to work.

在dev分支中先修复刚才的bug-02: git cherry-pick commit_id (e763105 below)

insert image description here

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

恢复工作现场,接着进行dev分支开发:git stash pop

强制删除一个没有被合并过的分支:git branch -D xxx

6. Multi-person collaboration to eliminate conflicts

Conflicts often occur when code is submitted, especially in the case of collaborative development. When merging branches, someone in the dev branch happens to have modified the same file, and git doesn't know which person's file should prevail, that is to say, different operations on the same file and the same location in the dev branch! So there was conflict.

git push produces a conflict, indicating that someone has synchronized his local code to the remote one step ahead of you. At this time, you need to pull the code first. You can use the command git pull, which will merge the remote submission with your local submission. If there is a conflict that needs to be resolved and submitted manually, a merge record will be generated.

git pull – rebase This command will “place” your commits after the remote pulled commits, i.e. change the base (rebase), if there are conflicts to resolve all conflicting files, git add <conflict file> git rebase --continue Perfect solution to the problem.

从本地推送分支:git push origin branch-name

拉取远程仓库代码:git pull

可能会抓取失败,需要指定本地dev分支与远程origin/dev分支的链接: git branch --set-upstream-to=origin/dev dev

抓取成功后,代码本地合并,我们需要手动解决冲突,再推送:git push origin dev

On GitHub, you can fork any open source repository, you have read and write permissions to the forked repository, you can clone it locally for modification, or you can push pull requests to the official repository to contribute code.

7. Tag Management

新建标签: git tag (the default is HEAD, which is on the latest commit, submit the commit first and then tag, or specify a commit id: git tag v0.9 f52c633)

指定标签信息:git tag -a -m “blablabla…”

查看所有标签:git day

推送本地标签:git push origin

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

删除一个本地标签:git tag -d

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

配置别名: Git config --global alias .st status (st display status)

Many people use co for checkout, ci for commit, br for branch and sw for switch

Guess you like

Origin blog.csdn.net/Better_Xing/article/details/123775158