Master Git quickly

1. Version control tool

1.1. What is a version control system?

Version Control System (Version Control System): It is a system that records changes in the content of one or several files for future reference to revisions of specific versions. Version control systems can be applied not only to text files of software source code, but also to version control of any type of file.

Common version control systems are: cvs, svn, git

1.2. Why is there a version control system?

  1. During the development process, it is often necessary to modify or even delete a file, but we also hope to save the history of this file. If it is backed up, the management will be very complicated.
  2. During multi-person development, if multiple people are required to cooperate to develop a page, modification and merging will be very tricky. Conflicts can easily arise.

1.3. Classification of version control systems

About version control

local version control system

The local version control system is to record the different changes of the version on one machine to ensure that the content will not be lost

Disadvantage: If multiple people develop, everyone develops on different systems and computers, and there is no way to work together.

Centralized version control system

Svn/cvs are both centralized version control systems

  1. Requires a central server to manage code versions and backups
  2. All user computers obtain codes from the central server or submit local codes to the central server
  3. Depending on the network environment, if you cannot connect to the central server, you cannot submit and obtain code.
  4. If the central server goes down, no one can work.

picture

Distributed version control system

git is a distributed version control system.

  1. Requires a server as a code repository
  2. Each user's computer is a server (code warehouse), and it is mirrored with the code warehouse. Users modify and obtain codes and submit them to their own servers.
  3. Work without internet.
  4. When connected to the network, users can choose to synchronize their own server with the code repository.

picture

Two, git

2.1. Introduction to git

Git is a free, open source distributed version control system for agile and efficient handling of any project, small or large.

Git is an open source version control software developed by Linus Torvalds to help manage Linux kernel development.

2.2. git installation

download link

#  推荐使用国内镜像下载 http://npm.taobao.org/mirrors/git-for-windows
1. 不要安装在中文目录
2. 不要使用桌面管理软件

The installation is very simple, just go to the next step. Right click on any directory, and the following picture will appear, indicating that the installation is successful.

picture

There are two main ways to use git,

  • git gui, that is, the way of graphical interface
  • git bash, the command line way

Bash is a command of the linux system, so before learning git, let's learn bash first

  1. In any folder, you can use the right button to open the command line window through git bash here, and the path at this time is the current directory.
  2. Right click on the window, and you can adjust the font size through options–>Text–>select.
# cd 改变目录  (change directory)
cd images   #进入images文件夹
cd ..      #进入上一层目录
cd ~       #进入用户根目录

# tab  自动补全,当我们输命令或者目录很长时,可以使用tab键进行自动补全。
# 按两次tab,会把所有符合要求的内容都列出来。

# pwd 打印当前目录的路径 (print work directory)
pwd


# ls 展示当前目录列表(list)
ls         # 展示当前目录
ls -a      # 展示全部内容,包括隐藏文件
ls -l      # 以列表的形式展示内容
ls -al     # 以列表的形式展示所有的内容,包括隐藏文件。
ls --help  # 查看ls所有的参数。
ls -l images   # 展示images目录下的文件,如果没有写目录,默认展示当前目录。


# clear reset清屏
clear  # 清除屏幕内容,滚动条,保留了历史
reset  # 重置,历史记录没了。


# mkdir  创建一个文件夹 (make directory)
mkdir css          # 创建一个css的文件夹
mkdir css img js   # 创建了三个文件夹

# rmdir  删除一个空的文件夹(没啥用)
rmdir img   # 删除文件夹

# touch  创建文件
touch index.html   #创建了一个index.html文件
touch css/index.css # 在css目录下创建idnex.css文件

# rm 删除一个文件获取文件夹
rm index.html # 删除index.html文件
rm js         # 删除空的js文件夹
rm -r css     # 递归删除一个文件夹

# mv 移动文件(move)
mv index.html js            # 将html文件移动到js文件夹中
mv index.html index2.html   # 将index.html重命名为index2.html

# cp 复制文件(cp)
cp index.html index2.html   # 复制index.html文件,命名为index2.html
cp -r css css02             # 如果复制的是文件夹,需要使用-r参数。

# cat 查看文件全部内容
cat index.html
# less 查看文件部分内容
less index.html
# q退出查看

2.3. Basic operation

  1. Initialize the git repository git init
  2. View the status of the current git repository git status
  3. Add files to git's temporary storage area git add file name
  4. Submit the file from the temporary storage area to the warehouse area git commit -m 'commit description'
  5. View the commit log git log
# 初始化git仓库,会在当前目录生成一个隐藏文件夹 .git  不要去修改这个文件夹下的任意东西。
git init

# 查看git的状态 ,如果此时新建一个文件,那么这个文件是没有被追踪的,说白了git还没有管理这个新建的文件
git status 

# 让git管理这个新建的文件
git add index.html

# 让文件由暂存区提交到仓库区。此时文件才真正的被git管理了。
# 如果提交日志乱码,右键-->options-->Text-->将编码改成utf-8
git commit -m '第一次提交'

# 查看提交日志
git log

2.4. Configure mailbox and username

If you use git for the first time, you will be asked to set the username and email

# git config  user.name 你的目标用户名
# git config  user.email 你的目标邮箱名
# 这种配置方式只有在当前仓库生效
git config user.name shuaige
git config user.email [email protected]

# 可以使用--global参数,配置全局的用户名和邮箱,这样别的git仓库就不需要重新配置了。
# 如果同时配置了局部的和全局的,那么局部的用户名和邮箱将会生效。
git config  --global user.name shuaige
git config  --global user.email [email protected]

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

2.5. How git works

picture

2.6. Detailed explanation of git commands

2.6.1. git add (emphasis)

  • Function: Add files from the workspace to the temporary storage area, and temporarily store files
  • Order:

git git add filename

  • For example: git add index.html
  • git add --all or git add -A or git add . (shorthand) add all files
  • git add a.txt b.txt adds two files at the same time
  • git add *.js Add all js files in the current directory

2.6.2. git checkout filename

  • Function: The contents of the temporary storage area are restored to the work area.
  • git checkout 1.txt restores the 1.txt file in the temporary storage area to the work area

2.6.3. git commit (emphasis)

  • Function: Add files from the temporary storage area to the warehouse area
  • git commit -m "Commit Notes"

2.6.4. git status

  • Function: View the status of the file
  • Command: git status
  • Command: git stauts -s to simplify the log output format

2.6.5. git log

  • Function: view the commit log
  • git log can only view the current head and previous logs
  • git log --oneline concise log information
  • git reflog View all commit changelogs

2.6.6. git reset

  • Function: version rollback, restore the code to a version that has been submitted.
  • git reset --hard version number rolls back the code to a specified version (as long as the version number has the first few digits)
  • git reset --hard head~1

Roll back the version to the last commit

  • ~1: last commit
  • ~2: last commit
  • ~0: current commit

2.7. git ignore files

In the warehouse, some files do not want to be managed by git, such as data configuration passwords, some ideas for writing codes, etc. Git can be configured to ignore some files, so that these files do not need to be submitted.

  • Create a .gitignore file in the root directory of the warehouse, and the file name is fixed.
  • Add file paths that do not need to be managed by git to .gitignore
# 忽视idea.txt文件
idea.txt

# 忽视.gitignore文件
.gitignore

# 忽视css下的index.js文件
css/index.js

# 忽视css下的所有的js文件
css/*.js

# 忽视css下的所有文件
css/*.*
# 忽视css文件夹
css

Three, git branch operation

A branch is a parallel universe in a sci-fi movie. When you are trying to learn Git in front of the computer, another you are trying to learn SVN in another parallel universe.

If the two parallel universes do not interfere with each other, it will have no effect on you now. However, at some point, the two parallel universes merged, and as a result, you learned both Git and SVN!

picture

3.1. Why branch?

  • If you want to develop a new function, it takes 2 weeks. You can only write 50% of the code in the first week. If you submit it immediately, the code is not finished, and the incomplete code will affect others and cannot work. If you wait until the code is finished and then submit it, the code is easily lost and the risk is high.
  • With a branch, you can create a branch of your own, which cannot be seen by others and will not affect others. You work on your own branch, submit to your own branch, and merge it into the original branch once the function is developed. This is safe and does not affect the work of others.
  • In the process of work, we often encounter the situation of multi-task parallel development , and the use of branches can well avoid the impact between tasks.
  • Other versions of tools such as svn and cvs also have the concept of branching, but the branching operations in these tools are very slow, just like decoration.

3.2. Commands for branch operations

3.2.1. Create a branch

  • git branch branch name creates a branch, and the code in the branch is exactly the same as the content of the current branch when it is created.
  • When git commits for the first time, there is a master branch called master.

3.2.2. View branch

  • git branch can view all branches,
  • There will be a * in front of the current branch

3.2.3. Switch branches

  • git checkout branch name switch branch
  • Any operation on the current branch will not affect other branches, unless the branches are merged.
  • Before switching branches, you must ensure that the code has been submitted

3.2.4. Create and switch branches

  • git checkout -b branch name to create and switch branches

3.2.5. Delete branch

  • git branch -d branch name can delete the branch
  • Note: You cannot delete the current branch in the current branch, you need to switch to other branches to delete.
  • Note: The master branch can be deleted, but it is not recommended.

3.2.6. Merging branches

  • git merge branch-name merges the contents of other branches into the current branch.
  • Execute git merge dev in the master branch to merge the code in the dev branch into the master branch

3.3. How git branches work

picture

3.4. git merge conflicts

  • For the same file, if there are multiple branches that need to be merged, conflicts are prone to occur.
  • When merging branches, if there is a conflict, you can only handle it manually and submit it again. In general, you can put your own code behind the conflicting code.

4. Remote warehouse

All programmers can share the version through the remote warehouse to achieve the same effect of everyone's code.

4.1. Commands related to remote warehouse

4.1.1. git push

  • Role: Submit local code to remote warehouse
  • git push warehouse address master Submit the code to the remote warehouse, note that the master branch must be written and cannot be omitted
  • Example: git push [email protected]:hucongcong/test.git master If you use it for the first time, you need to fill in the username and password of github

4.1.2. git pull

  • Function: download the remote code to the local
  • git pull code address downloads the master branch in the remote code to the local
  • Usually before pushing, you need to pull once.

4.1.3. git clone

  • Function: clone the code of the remote warehouse to the local
  • git clone warehouse address to customize the local warehouse name to clone the entire warehouse to the local

4.1.4. git remote

Every push and pull operation needs to bring the address of the remote warehouse, which is very troublesome. We can set an alias for the warehouse address

  • git remote add warehouse alias warehouse address

Use the warehouse alias instead of the warehouse address. The warehouse alias is equivalent to a variable, and the warehouse address is the corresponding value.

  • git remote add hucc [email protected]:hucongcong/test.git Set up a hucc warehouse alias, in the future push and pull can use hucc instead of the warehouse address
  • git remote remove hucc deletes the warehouse alias of hucc.
  • git remote View all warehouse aliases
  • If the git clone command is used to obtain it from the remote warehouse, then the local warehouse will automatically add a remote address of origin, pointing to the remote address of the clone.

4.2. github

git is not directly related to github.

  • git is a version control tool.
  • GitHub is a code hosting platform and a remote code warehouse of git.
  • When working in the future, the company will have its own code repository.

github official website

Open source China-git code cloud

1. gitHub是一个面向开源及私有软件项目的托管平台,因为只支持git 作为唯一的版本库格式进行托管,故名gitHub。
2. github免费,代码所有人都能看到,但是只有你自己能修改。付费的可以隐藏。

Create a project on github and get the address of the warehouse. Then you can push the local code to the remote server.

4.3. SSH password-free login

(symmetric encryption and asymmetric encryption)

Every time you push the code, you need to enter the user name and password, which is very troublesome. So we can configure an SSH password-free login.

  • For the security of the account, github needs to verify the identity of the user for each push request, and only legitimate users can push
  • Use ssh to achieve password-free operation (no need to use a password)

4.3.1. SSH password-free login configuration

  • 1 Create an SSH Key: ssh-keygen -t rsa
  • 2 Find the .ssh folder in the file path C:\Users\Current Username\
  • 3 There are two files in the folder:
  • Private key: id_rsa
  • Public key: id_rsa.pub
  • 4 In the github -> settings -> SSH and GPG keys page, create a new SSH key
  • 5 Paste the content of the public key id_rsa.pub into the corresponding text box
  • 5 Create a new warehouse in github or use the current warehouse, get [email protected]: username/warehouse name.git
  • 6 After that, "communicate" with github again in SSH mode, no need to enter a password to confirm the identity

5. Idea integrates git

5.1.idea configuration git

First, make sure that the git client has been installed locally, and idea will automatically detect it. If not, or if you want to make manual adjustments, you need to reconfigure here. After the configuration is successful, you can click the Test button to test whether it is OK!

picture

5.2: Idea uploads the project to the local warehouse

picture

picture

Right-click the specified project, select Git to add the project to version control, and then use Commit Directory to submit the project to the local warehouse.

picture

picture

picture

5.3: Push the local warehouse project to the remote warehouse

picture

picture

picture

Note: If the ssh public key is not configured, you need to enter the GitHub or gitee remote warehouse username and password for the first local push.

5.4: idea sets git to ignore some files

0. Install the .ignore plugin

Click File->Settings, find Plugins search ignore, then install, OK restart idea

1. Create a project and add a .gitignore file to the project

picture

picture

picture

Suggested .gitignore file content

######################################################################
# Build Tools
.gradle
/build/
!gradle/wrapper/gradle-wrapper.jar

target/
!.mvn/wrapper/maven-wrapper.jar

######################################################################
# IDE
### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans

### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr
out
gen

### NetBeans ###
nbproject/private/
build/*
nbbuild/
dist/
nbdist/
.nb-gradle/

######################################################################
# Others
*.log
*.xml.versionsBackup

!*/build/*.java
!*/build/*.html
!*/build/*.xml
# .gitignore文件内容编写规则
*.a       # 忽略所有 .a 结尾的文件
!lib.a    # 但 lib.a 除外
/TODO     # 仅仅忽略项目根目录下的 TODO 文件,不包括 subdir/TODO
build/    # 忽略 build/ 目录下的所有文件
doc/*.txt # 会忽略 doc/notes.txt 但不包括 doc/server/arch.txt

2. Create a local warehouse

$ git init

3. Create a personal branch

$ git checkout -b dev

4. Add files to version control and submit to local warehouse

$ git add .
$ git commit -m "first commit"

5. Push to the remote warehouse

$ git push  https://gitee.com/liyunyi/ssm.git dev

6. Push the merged branch to the main branch

$ git checkout master
$ git merge dev
$ git push https://gitee.com/liyunyi/ssm.git master

Attachment: Command Encyclopedia

insert image description here

  • Workspace: Workspace
  • Index / Stage: temporary storage area
  • Repository: warehouse area (or local warehouse)
  • Remote: remote warehouse

1. Warehouse

# 在当前目录新建一个Git代码库
$ git init

# 新建一个目录,将其初始化为Git代码库
$ git init [project-name]

# 下载一个项目和它的整个代码历史
$ git clone [url]

2. Configuration

# 显示当前的Git配置
$ git config --list

# 编辑Git配置文件
$ git config -e [--global]

# 设置提交代码时的用户信息
$ git config [--global] user.name "[name]"
$ git config [--global] user.email "[email address]"

3. Add/delete files

# 添加指定文件到暂存区
$ git add [file1] [file2] ...

# 添加指定目录到暂存区,包括子目录
$ git add [dir]

# 添加当前目录的所有文件到暂存区
$ git add .

# 添加每个变化前,都会要求确认
# 对于同一个文件的多处变化,可以实现分次提交
$ git add -p

# 删除工作区文件,并且将这次删除放入暂存区
$ git rm [file1] [file2] ...

# 停止追踪指定文件,但该文件会保留在工作区
$ git rm --cached [file]

# 改名文件,并且将这个改名放入暂存区
$ git mv [file-original] [file-renamed]

4. Code submission

# 提交暂存区到仓库区
$ git commit -m [message]

# 提交暂存区的指定文件到仓库区
$ git commit [file1] [file2] ... -m [message]

# 提交工作区自上次commit之后的变化,直接到仓库区
$ git commit -a

# 提交时显示所有diff信息
$ git commit -v

# 使用一次新的commit,替代上一次提交
# 如果代码没有任何新变化,则用来改写上一次commit的提交信息
$ git commit --amend -m [message]

# 重做上一次commit,并包括指定文件的新变化
$ git commit --amend [file1] [file2] ...

5. Branch

# 列出所有本地分支
$ git branch

# 列出所有远程分支
$ git branch -r

# 列出所有本地分支和远程分支
$ git branch -a

# 新建一个分支,但依然停留在当前分支
$ git branch [branch-name]

# 新建一个分支,并切换到该分支
$ git checkout -b [branch]

# 新建一个分支,指向指定commit
$ git branch [branch] [commit]

# 新建一个分支,与指定的远程分支建立追踪关系
$ git branch --track [branch] [remote-branch]

# 切换到指定分支,并更新工作区
$ git checkout [branch-name]

# 切换到上一个分支
$ git checkout -

# 建立追踪关系,在现有分支与指定的远程分支之间
$ git branch --set-upstream [branch] [remote-branch]

# 合并指定分支到当前分支
$ git merge [branch]

# 选择一个commit,合并进当前分支
$ git cherry-pick [commit]

# 删除分支
$ git branch -d [branch-name]

# 删除远程分支
$ git push origin --delete [branch-name]
$ git branch -dr [remote/branch]

6. Label

# 列出所有tag
$ git tag

# 新建一个tag在当前commit
$ git tag [tag]

# 新建一个tag在指定commit
$ git tag [tag] [commit]

# 删除本地tag
$ git tag -d [tag]

# 删除远程tag
$ git push origin :refs/tags/[tagName]

# 查看tag信息
$ git show [tag]

# 提交指定tag
$ git push [remote] [tag]

# 提交所有tag
$ git push [remote] --tags

# 新建一个分支,指向某个tag
$ git checkout -b [branch] [tag]

7. View information

# 显示有变更的文件
$ git status

# 显示当前分支的版本历史
$ git log

# 显示commit历史,以及每次commit发生变更的文件
$ git log --stat

# 搜索提交历史,根据关键词
$ git log -S [keyword]

# 显示某个commit之后的所有变动,每个commit占据一行
$ git log [tag] HEAD --pretty=format:%s

# 显示某个commit之后的所有变动,其"提交说明"必须符合搜索条件
$ git log [tag] HEAD --grep feature

# 显示某个文件的版本历史,包括文件改名
$ git log --follow [file]
$ git whatchanged [file]

# 显示指定文件相关的每一次diff
$ git log -p [file]

# 显示过去5次提交
$ git log -5 --pretty --oneline

# 显示所有提交过的用户,按提交次数排序
$ git shortlog -sn

# 显示指定文件是什么人在什么时间修改过
$ git blame [file]

# 显示暂存区和工作区的差异
$ git diff

# 显示暂存区和上一个commit的差异
$ git diff --cached [file]

# 显示工作区与当前分支最新commit之间的差异
$ git diff HEAD

# 显示两次提交之间的差异
$ git diff [first-branch]...[second-branch]

# 显示今天你写了多少行代码
$ git diff --shortstat "@{0 day ago}"

# 显示某次提交的元数据和内容变化
$ git show [commit]

# 显示某次提交发生变化的文件
$ git show --name-only [commit]

# 显示某次提交时,某个文件的内容
$ git show [commit]:[filename]

# 显示当前分支的最近几次提交
$ git reflog

8. Remote synchronization

# 下载远程仓库的所有变动
$ git fetch [remote]

# 显示所有远程仓库
$ git remote -v

# 显示某个远程仓库的信息
$ git remote show [remote]

# 增加一个新的远程仓库,并命名
$ git remote add [shortname] [url]

# 取回远程仓库的变化,并与本地分支合并
$ git pull [remote] [branch]

# 上传本地指定分支到远程仓库
$ git push [remote] [branch]

# 强行推送当前分支到远程仓库,即使有冲突
$ git push [remote] --force

# 推送所有分支到远程仓库
$ git push [remote] --all

9. Revocation

# 恢复暂存区的指定文件到工作区
$ git checkout [file]

# 恢复某个commit的指定文件到暂存区和工作区
$ git checkout [commit] [file]

# 恢复暂存区的所有文件到工作区
$ git checkout .

# 重置暂存区的指定文件,与上一次commit保持一致,但工作区不变
$ git reset [file]

# 重置暂存区与工作区,与上一次commit保持一致
$ git reset --hard

# 重置当前分支的指针为指定commit,同时重置暂存区,但工作区不变
$ git reset [commit]

# 重置当前分支的HEAD为指定commit,同时重置暂存区和工作区,与指定commit一致
$ git reset --hard [commit]

# 重置当前HEAD为指定commit,但保持暂存区和工作区不变
$ git reset --keep [commit]

# 新建一个commit,用来撤销指定commit
# 后者的所有变化都将被前者抵消,并且应用到当前分支
$ git revert [commit]

# 暂时将未提交的变化移除,稍后再移入
$ git stash
$ git stash pop

10. Others

# 生成一个可供发布的压缩包
$ git archive

Guess you like

Origin blog.csdn.net/weixin_44618297/article/details/130346313