The basic operations that git needs to learn

1. Installation

https://git-scm.com

2. Configuration

The first step after installation gitis to configure (set your user name and email). This information will be used for every gitcommit and cannot be changed.

git config user.name "你的姓名"
git config user.email "你的邮箱"

Through --globalcan set the global configuration information.

git config --global user.name "你的姓名"
git config --global user.email "你的邮箱"

check configuration

//打印所有config
git config --list
//打印指定config
git config user.name
git config user.email

3. Create a warehouse

Go to your project directory and enter the following command to initialize a gitwarehouse.

git init

This command will create a .gitsubdirectory of . This contains all the necessary files to initialize the git repository, and this subdirectory is hidden. It is very important and cannot be easily changed.

4. Workflow and basic operations

When a project is gitinitialized, it only means that we want gitto manage this project, but the existing files or newly added files in this project have not yet entered the version control management, and they are all untracked (Untracked) .

4.1 Necessary basic operations (necessary for work)
1. Check the file status of the workspacegit status
git status
2. Add workspace files to the staging areagit add
//添加一个文件
git add 1.txt
//添加多个文件
git add 2.txt 3.txt
//添加整个目录
git add ./one
//添加多个目录
git add ./two ./three
//添加所有文件
git add .
3. Create versiongit commit

Submit the changes in the staging area to the local gitrepository. Each commit produces a 40one-bit hash of the commit id.

git commit -m '给这次提交取一个备注名字'
4. Pull the codegit pull

command is used to fetch code from remote and merge local version. git pullIn fact, it is shorthand for git fetchand git mergeFETCH_HEAD.

//将远程主机 origin 的 master 分支拉取过来,与本地的 brantest 分支合并。
git pull origin master:brantest
//如果远程分支是与当前分支合并,则冒号后面的部分可以省略。
git pull origin master
5. Push codegit push

It is used to upload and merge the local branch version to the remote.

//git push 命用于从将本地的分支版本上传到远程并合并。
git push origin master

If you want to omit origin master, see 7.3, link local branch with remote branch of the same name.

4.2 Other basic operations
6. Check the commit loggit log
//完整格式
git log
//单行日志 输出id+备注名
git log --oneline
7. Fix commitgit commit --amend

Fix (append) commits, which append newly modified code to the previous commit without adding a new commit.

git commit --amend -m '备注'
8. Deletegit rm
//从git仓库与工作区中删除指定文件(彻底删除)
git rm 文件
//只删除git仓库中的文件(不想这个文件被追踪了)untracked
git rm --cached 文件
//rm以后,需要进行一次commit,否则rm将保留在暂存区中
git commit -m '删除了以一些文件'
9. Undo Resetgit reset

Undo from staging area to work area

//从暂存区中撤销一个指定文件
git reset HEAD 文件
//从暂存区中撤销所有文件
git reset HEAD .

fallback version

//回退到commitID版本
git reset --hard commitID(之前commit生成的那个40位的哈希值)
//回去了反悔了,想要最新的代码git pull即可
10. Comparegit diff
//比较工作区和暂存区
git diff 文件
//比较暂存区和仓库
git diff --cached [commitID] 文件
//比较工作区和仓库
git diff commitID filename
//比较仓库不同版本
git diff commitID1 commitID2

5. branch

5.1. View branch
git branch
5.2. Create a branch
git branch 分支名
5.3. Switch branches
git checkout 分支名
//也可以使用git checkout -b 来新建分支(创建并到新创建的分支下)
git checkout -b 分支名
5.4. Branch merge
//B分支合并到A分支,需要先切换到A分支再进行合并
git merge B(被合并分支)
//查看已经合并的分支
git branch --merged
//查看未合并的分支
git branch --no-merged
5.5. Delete branch
//如果分支为未合并状态,则不允许删除
git branch -d 分支名称
//强制删除
git branch -D 分支名称

6. Labels

Sometimes, we want to put some tags on a particular commit. The label can be regarded as an alias of a certain version, because gitthe version number of the version is represented by a 40-bit hash value. For the convenience of management, gityou can label the version, which is equivalent to taking an alias.

6.1 Create tags
//给当前最新提交的commit打上标签
git tag v1.0.0
//给之前的commit提交打标签,在此之前先找到commitID(40位哈希值)
git tag v0.5.0 CommitID
//还可以创建带有说明的标签,用-a指定标签名,-m指定说明文字(基于最新的commit提交)
git tag -a v1.0.0 -m '第一版'
//为历史commit创建带有说明的标签
git tag -a v1.0.0 -m '第一版' commitID
6.2 View all tags
git tag
6.3 Delete tags
git tag -d 标签名

The created tags are only stored locally and will not be automatically pushed to the remote.

6.4 Push tags to remote
// 推送单个Tag
git push origin 标签名
// 推送所有本地Tag
git push origin --tags  

6.5 Delete Remote Tag

If you want to delete the tag pushed to the remote, you need to delete the local tag first, and then re-push to the remote code repository. .

//删除本地标签
git tag -d 标签名
//删除远程标签
git push origin :refs/tags/标签名
6.6 View tag details
git show 标签名

7. Git remote warehouse, configure SSH

Since the transmission between your local gitwarehouse and the remote gitwarehouse is encrypted by SSH, we need to configure SSH. The configuration of the SSH key is not necessary. If it is not configured, only the HTTPS protocol can be used. In this way, the user name and password need to be entered for each submission. After configuration it is not required. The principle of configuring the SSH key is very simple. Asymmetric encryption is used to generate the public key and private key. The public key tells the private githubkey to stay on your own computer (not to be leaked). When we submit githubdata to the githubHis public key encrypts a message and returns it to our computer. If we can successfully decrypt it with the private key, it means that he is a legitimate user.

How to configure?

7.1 First check whether there is a key locally
(1) Open the terminal and enter the command
cd ~/.ssh
ls

If the output has the following files, it means that the local key is contained, otherwise it does not.insert image description here

(2) If there is a key, enter the following command to view the public key

The output is the public key.

cat ~/.ssh/id_rsa.pub
(3) If there is no key, enter the following command to generate
$ ssh-keygen -t rsa -C "邮箱地址"

The email address is the email address you registered on Github, and then you will be asked to confirm the path and enter the password. We just use the default one and press Enter all the way.
After generation, you can perform (2) view

(4) Add the SSH public key in the settings in github

insert image description here

(5) Associate the local warehouse with the remote warehouse
git remote add origin 远程仓库地址

This command git automatically sets the name of the remote warehouse to origin.

(6) View remote warehouse
git remote
7.2 Pull code
//将github数据拉取到本地当前分支
git pull
//拉取到本地master分支
git pull origin master 
7.3 Push to remote warehouse
//将本地master分支推送到远程origin的master分支上,-u用于第一次推送,表示两个master分支关联
git push -u origin master

-uoriginThe parameter can set the branch of the warehouse masteras the upstream of the current branch of the local warehouse while pushing . After adding this parameter, git pullwhen you run the command to pull code in the future, this branch of the local warehouse can directly obtain the content from the branch origin. masterThis parameter is only pushadded at the first time, and git pushit can be used directly in the future.

Guess you like

Origin blog.csdn.net/honeymoon_/article/details/124378394