git command

Common git commands

Synchronize the latest state of the repository and merge

a) git pull: get the latest version from the remote and merge it to the local repository (git fetch+git merge)

git pull <远程库名> <远程分支名>:<本地分支名>  
git pull origin dev

or
b)

1. Synchronize to the latest state of the warehouse

git fetch <远程库名> <远程分支名>:<本地分支名>  
git fetch origin dev

2.merge or rebase

git checkout dev
git merge origin/dev
git checkout dev
git rebase origin/dev

The difference between git merge and git rebase

  1. git merge
    write picture description here
  2. git rebase
    write picture description here

write picture description here

Conflict resolution git merge & git rebase

git merge

  1. Discover the conflict file readme.txt through git state
  2. After modifying readme.tx, git add readme.txt
  3. commit git commit -m 'conflict fixed'

git rebase

Upload to github repository

About git push

git push <远程主机名> <本地分支名>:<远程分支名>
# 建立关联,设置本体(origin)为[email protected]:doublespending/gitTest.git
git remote add origin git@github.com:doublespending/gitTest.git
# 上传本地分支dev到相应的origin上的dev分支,不存在则新建
git push -u origin dev

Workspace & Staging Area & Repository Operations

  1. Modification: When you change the contents of a file in the workspace and want to discard the modifications in the workspace directly, use the command git checkout – file.
    2. Modification: When you not only change the content of a file in the workspace, but also add it to the temporary storage area, you want to discard the modification. It is divided into two steps. The first step is to use the command git reset HEAD file, and then use git checkout – file
  2. Modification: When inappropriate modifications have been submitted to the repository, if you want to undo this submission, use git reset – hard commit_id (^HEAD) to roll back the version
  3. git rm for you to confirm deletion of a file

All version control systems can only track changes in text files , such as TXT files, web pages, all program code, etc. Git is no exception. Version control systems can tell you every change, such as adding the word "Linux" on line 5 and deleting the word "Windows" on line 8. Although binary files such as pictures and videos can also be managed by the version control system, they cannot track the changes of the files. They can only string together each change of the binary files , that is, only know that the picture has been changed from 100KB to 120KB. What has been changed, the version control system does not know and has no way of knowing.

Workspace/Staging/Repository

write picture description here
look here

local warehouse

# 设置个人信息
git config --global user.email "[email protected]"
git config --global user.name "Your Name"

# 把这个目录变成Git可以管理的仓库
git init
# 把文件file1, file2, file3添加到仓库,实际上就是把文件修改添加到暂存区
git add file1
git add file2 file3
# 把三个文件提交至仓库并写上注释"add 3 files.",实际上就是把暂存区的所有内容提交到当前分支
git commit -m "add 3 files."

# 需要提交的文件修改通通放到暂存区,然后,一次性提交暂存区的所有修改。

# 修改README.md后,通过git status命令可以让我们时刻掌握仓库当前的状态,上面的命令告诉我们,readme.txt被修改过了,但还没有准备提交的修改。
git status
# git diff告诉我们仓库下文件的修改情况
git diff
git diff README.md

git diff 比较的是工作区和暂存区的差别
git diff --cached 比较的是暂存区和版本库的差别
git diff HEAD 可以查看工作区和版本库的差别

# 添加到仓库
git add README.md

# 提交至仓库前查看情况
git status

# 提交至仓库
git commit -m "modify something"

git status

# 查看修改的历史记录
git log
git log --pretty=oneline

# 回退到上一个版本HEAD^,上上个版本HEAD^^
git reset --hard HEAD^

# 最新版本的信息不见了
git log

# 回到最新版本
git reset --hard 最新版本id的前几位

# 记录每一次命令,以及commit的id
git reflog

# 让README.md回到最近一次git commit或git add时的状态,"--"很重要
git checkout -- README.md

# 当你不但改乱了工作区某个文件的内容,还添加到了暂存区时,想丢弃修改
git reset HEAD README.md # 清空暂存区
git checkout -- README.md

# 在工作空间以及版本库中删除TEST.md
git rm TEST.md
git commit -m "remove TEST.md"


Merge multiple commits (commit)

gitk

gitk is a graphical viewer for history, you can think of it as a powerful graphical interface based on git log and git grep commands, when you need to find a record that happened in the past, or visualize project history , you will use this tool.

gitk

remote warehouse

SSH Key

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

Copy the generated public key (id_rsa.pub) to github.

Associate a remote library

git remote add origin git@server-name:path/repo-name.git

Push all the contents of the master branch to the remote repository for the first time

To push the contents of the local library to the remote, use the git push command to push the current branch master to the remote.

Since the remote library is empty, when we push the master branch for the first time, we add the -u parameter. Git will not only push the contents of the local master branch to the remote new master branch, but also push the local master branch and the remote master branch. The master branch is linked to simplify commands for future pushes or pulls.

git push -u origin master

push new changes

git  push origin master

clone from remote repository

git clone (ssh/https link)

Create and merge branches

Create dev branch, then i switch to dev branch

git checkout -b dev

# git checkout命令加上-b参数表示创建并切换,相当于以下两条命令:
git branch dev
git checkout dev

View current branch

git branch

Merge the work of the specified branch (dev) into the current branch (master) branch

git merge dev

Parameters for git merge

--no-ff指的是强行关闭fast-forward方式。  

fast-forward方式就是当条件允许的时候,git直接把HEAD指针指向合并分支的头,完成合并。属于“快进方式”,不过这种情况如果删除分支,则会丢失分支信息。因为在这个过程中没有创建commit  

git merge --squash 是用来把一些不必要commit进行压缩,比如说,你的feature在开发的时候写的commit很乱,那么我们合并的时候不希望把这些历史commit带过来,于是使用--squash进行合并,此时文件已经同合并后一样了,但不移动HEAD,不提交。需要进行一次额外的commit来“总结”一下,然后完成最终的合并。  

总结:  
--no-ff:不使用fast-forward方式合并,保留分支的commit历史  
--squash:使用squash方式合并,把多次分支commit历史压缩为一次  

–no-ff Illustration: It can be found that git merge directly points HEAD to the feature branch, while git merge –no-ff will use merge as a commit. So there are 3+1=4 on the left and 3 on the right.
write picture description here

delete the dev branch

git branch -d dev

conflict resolution

git merge two conflicting branches, you can use git status to know the conflicting files

git status

You can use cat to output the conflict file to get the following

HAHA!
<<<<<<< HEAD
& simple
=======
AND simple!
>>>>>>> featurel

scene

The general steps to resolve conflicts are:

  1. First submit the code directly locally: git push origin master
  2. If someone else submits changes before you, git will prompt that the push fails, you need to pull the remote code first: git pull origin/master (pull the remote warehouse for automatic merge)
    1. If it can be automatically merged, git will prompt that the auto merge is successful, then you can directly git push origin master
    2. If it cannot be merged automatically, git will prompt that the auto merge failed, and the conflict needs to be resolved manually:
      1. git status to view conflicts
      2. Modify conflict
      3. git add .
      4. git commit -m 'Resolve conflicting comments'
      5. git push origin master

View the branch merge graph

git log --graph

branch strategy

write picture description here

bug branch

When checkout converts branches, we need to use git stash to save the workspace and staging area, because when we switch checkout, the current content will be overwritten.

git checkout -b dev
do some work (with no commit)
git stash
git checkout -b bug
fix bug
git commit -m "fix bug"
git checkout dev
git stash pop

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325810605&siteId=291194637