Basic use of git

Schematic diagram of git workflow

write picture description here

basic concept

3 steps

write picture description here
Under normal circumstances, our workflow is 3 steps, corresponding to the 3 arrow lines in the above figure:

// 把所有文件放入暂存区
git add .
// 把所有文件从暂存区提交进本地仓库
git commit -m "comment"
// 把所有文件从本地仓库推送进远程仓库
git push

4 districts

1.工作区(Working Area)
2.暂存区(Stage)
3.本地仓库(Local Repository)
4.远程仓库(Remote Repository)

5 states

In the above 4 areas, after entering each area successfully, a state will be generated, plus the most initial state, there are a total of 5 states. We name these five states as follows:

1.未修改(Origin)
2.已修改(Modified)
3.已暂存(Staged)
4.已提交(Committed)
5.已推送(Pushed)

Configure user information

// 配置用户名
git config --global user.name 用户名
// 配置邮箱
git config --global user.email 邮箱

Create a repository

1.1. Clone a repository from a remote host

git clone https://github.com/xxx/abc.git

1.2. Clone a repository from a remote host and re-specify the local directory name (for example, change to cdf)

git clone https://github.com/xxx/abc.git cdf

1.3. The complete creation process (project already exists)
Create the address of the git project on the Git website, for example, finally get the following git address: https://github.com/xxx/abc.git , and then through the git command line, Enter the root directory of the project (if you want to ignore some files, you need to create a file in the project root directory: .gitignore, which stores the files or folders to be filtered, such as: /bin)

// 初始化本地仓库
git init
// 添加到缓存区
git add .
// 提交到本地仓库
git commit -m "first commit"
// 添加远程仓库地址
git remote add origin https://github.com/xxx/abc.git
// 提交到远程仓库的master分支
git push -u origin master

Amend and submit

2.1. View status

git status

2.2. View the changed content It
should be noted that git diff related commands cannot be viewed for untracked files (such as newly added files, but git add has not been executed), such files can only be viewed with git status
2.2.1. and changes to the workspace (git add was not executed.)

git diff

2.2.2. View and cache changes (execute git add .)

git diff --cached

2.2.3. View and remote branch changes (such as master branch)

 git diff origin/master

2.2.4. View and a change of a commit (6b09161f1a4eb6b7cf4b3f1ea0c249009249002d is the commit id)

git diff 6b09161f1a4eb6b7cf4b3f1ea0c249009249002d

2.3. Track all changed files

git add .

2.4. Track the specified files (a.java and b.java)

git add a.java b.java

2.5, delete files

git rm a.java

2.6. Stop tracking files without deleting files (stop tracking folder bin and all its subfiles)

git rm -r --cached bin

2.7. Ignore the file (if the file has already been submitted, the instruction in 2.6 must be executed first. Here is an example of ignoring the bin folder)

.gitignore文件中添加/bin并保存

2.8. Submit all updated documents

git commit -m "描述信息"

View the commit log

3.1. View the commit log

git log

3.2, view the commit log (one line display)

git log --oneline

3.3. View submissions containing specific keywords (including abc or qq)

git log --grep=abc --grep=qq

3.4. View the change log of the remote branch (master) and merge it into the local

// 获取远程分支的master分支
git fetch origin master
// 查看远程分支更新日志
git log -p master..origin/master
// 合并远程分支的master分支到本地
git merge --no-ff origin/master

3.5. View the content of a submission (abc)

git show abc

Branches and Tags

4.1, show all branches

git branch -a

4.2. Switch to the specified branch or label (develop branch)

git checkout develop

4.3. Roll back to a historical version (9b7b412) and create a branch (v1.0.9)

git checkout -b v1.0.9 9b7b412

4.4. Create a new branch (develop)

git branch develop

4.5. Create a new local branch and submit it to the remote branch (develop branch)

// 创建develop分支
git checkout -b develop
// 提交到远程develop分支
git push origin develop

4.6, create a remote branch to the local (dev)

git checkout -b dev origin/dev

4.7. Associate the local branch dev to the remote origin/dev branch

git branch --set-upstream dev origin/dev

4.8, delete the local branch (dev branch)

git branch -d dev

4.9, delete the remote branch (dev branch)

git push origin --delete dev

4.10. List all local tags

git tag

4.11. Create a local tag based on the latest commit (v1.0)

git tag v1.0

4.12. Delete local tags (v1.0)

git tag -d v1.0

4.13. Push all local tags to remote

git push origin --tags

4.14. Push local tags (v1.0) to remote

git push origin v1.0

4.15. Delete remote label (v1.0)

git push origin :refs/tags/v1.0

merge

5.1. Merge the specified branch to the current branch (develop branch)

git merge --no-ff develop

5.2. Merge the content of the develop branch into the master branch, and the master does not copy the commit record of the develop, but creates a new commit record by itself (version 1.0)

// 先切换到develop分支
git checkout master
// 合并develop分支,但是不复制develop的提交记录
git merge --squash --no-commit develop
// 提交到更新到本地
git commit -m "version 1.0"
// 提交到远程仓库
git push

undo

6.1. Undo changes that have been added to the cache (but there are still changes in the workspace)

git reset

6.2, undo the local repair, get the file from the remote library to overwrite the local

// 撤销本地修复,并且工作去的变更也会清除
git reset --hard
// 从远程库获取文件覆盖本地
git pull

6.3. Roll back to the specified commit version (abcdefg)

git reset --hard abcdefg

6.4. Undo the specified commit (abcdefg)

git revert abcdefg

Remote operation

7.1. View the URL of the remote host

git remote

7.2. Rename the remote host (change origin to myOrigin)

 git remote rename origin myOrigin

7.3. Get files from remote library to local without merging (master branch)

git fetch origin master

7.4. Get files from remote repository and quickly merge (master branch)

git pull origin master

7.5. Submit files to remote and quickly merge (master branch)

git push origin master

7.6. Clean up remote branches and delete remote branches that do not exist locally

git remote prune origin

other

8.1. Save the current work site

// 执行一个git stash,stash队列中就会增加一个现场
git stash

8.2, view the stash queue

git stash list

8.3. For a restored work site, the restored site will be cleared (you can view several sites through git stash list)

// num就是要恢复的工作现场的编号
git stash pop stash@{num}

8.4. Restore stash@{0} in the stash queue, that is, the top job site

git stash pop

8.5. A work site restored, but not cleared

git stash apply stash@{num}

8.6. Empty the stash queue

git stash clear

refer to

1. Git's 4 stages of undoing changes
2. Git Stash method

Guess you like

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