GitHub project management detailed tutorial/git tutorial [with pictures and code]

Before using GitHub for project management,First, you need to create a warehouse yourself. If you use it yourself, you can set it to be private.

1. Basic operations of Git

The following 6 commands are commonly used in Git: git clone, git push, git add, git commit, git checkout, and git pull, which will be introduced in detail later.
Insert image description here
Common commands are as follows:

  • git add
git add # 添加文件到暂存区
(git add . 添加当前目录下的所有文件到暂存区)
  • git status
git status	#查看仓库当前的状态,显示有变更的文件。
(一般有参数 -s 来获得简短的输出结果:git status -s)
  • git diff
git diff	#比较文件的不同,即暂存区和工作区的差异。
(尚未缓存的改动:git diff
查看已缓存的改动: git diff --cached
查看已缓存的与未缓存的所有改动:git diff HEAD
显示摘要而非整个 diff:git diff --stat
)
  • git commit
git commit	#提交暂存区到本地仓库。
(一般有参数 -m [message] 可以是一些备注信息,
和-a 参数设置修改文件后不需要执行 git add 命令,直接来提交
例如git commit -am '修改 hello.php 文件' #可以省略git add 操作
  • git reset (can be used to clear files in the cache area [git add])
git reset	#回退版本。
(语法格式如下:
git reset [--soft | --mixed | --hard] [HEAD]
--mixed 为默认,可以不用带该参数,用于重置暂存区的文件与上一次的提交(commit)保持一致,工作区文件内容保持不变。
--soft 参数用于回退到某个版本。
--hard 参数撤销工作区中所有未提交的修改内容,将暂存区与工作区都回到上一次版本,并删除之前的所有信息提交。注意:谨慎使用 –-hard 参数,它会删除回退点之前的所有信息。
HEAD 说明:
	HEAD 表示当前版本
	HEAD^ 上一个版本
	HEAD^^ 上上一个版本
	HEAD^^^ 上上上一个版本
	以此类推...
可以使用 ~数字表示
	HEAD~0 表示当前版本
	HEAD~1 上一个版本
	HEAD^2 上上一个版本
	HEAD^3 上上上一个版本
	以此类推...
例如:
$ git reset HEAD^            # 回退所有内容到上一个版本  
$ git reset HEAD^ hello.php  # 回退 hello.php 文件的版本到上一个版本  
$ git  reset  052e           # 回退到指定版本
  • git rm
git rm	#将文件从暂存区和工作区中删除。
(例如
将文件从暂存区和工作区中删除:
git rm <file>
git rm runoob.txt 
如果删除之前修改过并且已经放到暂存区域的话,则必须要用强制删除选项 -f。
强行从暂存区和工作区中删除修改后的 runoob.txt 文件:
git rm -f runoob.txt 
如果想把文件从暂存区域移除,但仍然希望保留在当前工作目录中,换句话说,仅是从跟踪清单中删除,使用 --cached 选项即可:
git rm --cached <file>
git rm --cached runoob.txt
可以递归删除,即如果后面跟的是一个目录做为参数,则会递归删除整个目录中的所有子目录和文件:
git rm –r *

git mv #Move or rename workspace files.
git log #View historical commit records
git blame #View historical modification records of the specified file in list form
git remote #Remote warehouse operation
git fetch #Get the code library from the remote git
pull #Download the remote code and merge it
git push #Upload the remote code and merge it

2. Git configuration

git config has three directories:

  • /etc/gitconfigFile: A configuration in the system that is common to all users. If you use the --system option when using git config, this file will be read and written.
  • ~/.gitconfigFile: Configuration files in the user directory are only applicable to this user. If you use the --global option when using git config, this file will be read and written.
  • Configuration files in the Git directory of the current project (that is, files in the working directory .git/config): The configuration here is only valid for the current project. The configuration at each level will overwrite the same configuration at the upper level, so .git/configthe configuration in will overwrite /etc/gitconfigthe variable with the same name in .

1. Configure personal user name and email address

git config --global user.name "runoob"
git config --global user.email test@runoob.com

If the option is used --global, the changed configuration file is the one located in your user's home directory. All your future projects will use the user information configured here by default.

If you want to use a different name or email for a specific project去掉 --global 选项 , just reconfigure it and the new settings are saved in the current project's .git/config file.

2. View configuration information

To check existing configuration information, you can use git config --listthe command:

$ git config --list
http.postbuffer=2M
user.name=runoob
user.email=test@runoob.com

vim ~/.gitconfigYou can also view and modify it yourself.

3. Git workspace, staging area and repository

  • 工作区: It is the directory you can see on your computer.
  • 暂存区: In English it is called stage or index. It is generally stored in the index file ( ) in the .git directory .git/index, so we sometimes call the temporary storage area the index (index).
  • 版本库: There is a hidden directory in the workspace .git. This is not the workspace, but the Git repository.

The following figure shows the relationship between the workspace, the staging area in the repository, and the repository:
Insert image description here

  • The left side of the picture is the workspace, and the right side is the version library. The area marked "index" in the repository is the staging area (stage/index), and the area marked "master" is the directory tree represented by the master branch.

  • In the figure, we can see that "HEAD" at this time is actually a "cursor" pointing to the master branch. Therefore, where HEAD appears in the command shown in the figure, it can be replaced with master.

  • The area identified by objects in the figure is Git's object library, which is actually located in the ".git/objects" directory, which contains various created objects and content.

  • When a command is executed on a file modified (or added) in the workspace git add, the directory tree of the temporary storage area is updated, and at the same time, the content of the file modified (or added) in the workspace is written to a new object in the object library. , and the ID of the object is recorded in the file index of the temporary storage area.

  • When a commit operation (git commit) is performed, the directory tree of the temporary storage area is written to the version library (object library), and the master branch will be updated accordingly. That is, the directory tree pointed by master is the directory tree of the temporary storage area during submission.

  • When the command is executed git reset HEAD , the directory tree in the staging area will be rewritten and replaced by the directory tree pointed to by the master branch, but the workspace will not be affected.

  • When the command is executed git rm --cached <file>, the file will be deleted directly from the temporary storage area, and the work area will not be changed.

  • When the git checkout . or git checkout -- <file>command is executed, the files in the workspace will be replaced with all or specified files in the temporary storage area. This operation is dangerous and will clear changes in the workspace that have not been added to the staging area.

  • When the git checkout HEAD .or git checkout HEAD <file> command is executed, the files in the staging area and workspace will be replaced with all or part of the files in the master branch pointed to by HEAD. This command is also extremely dangerous, because it will not only clear the uncommitted changes in the workspace, but also clear the uncommitted changes in the staging area.

4. Upload your project to the GitHub repository

first

git remote add origin https://github.com/zhouzikang/test1.git
git branch -M main
git push -u origin main

Pay attention to changing it to your own GitHub project address.
If it does error: remote origin already exists., you can use:

git remote rm origin

the second time

git add .
git commit -m "修改说明"
git push

branch operation

  • In git, you can use the following command to view branches.
git branch # 查看所有本地分支
git branch -a # 同时查看远程分支
git branch -r # 只查看远程分支
git branch -av # 查看所有分支(本地和远程)并且标识当前正在使用的分支
git branch -avv # 查看所有分支(本地和远程)并且标识当前正在使用的分支,并且列出最后一次提交的信息
  • Create branch command:
git branch (branchname)
# 例如
$ git branch testing
  • Switch branch command:
git checkout (branchname)
# 例如
$ git checkout testing
Switched to branch 'testing'

When you switch branches, Git will replace the contents of your working directory with the last committed snapshot of that branch, so multiple branches don't require multiple directories.

We can also use the git checkout -b (branchname) command to create a new branch and immediately switch to the branch to operate in the branch.

$ git checkout -b newtest
Switched to a new branch 'newtest'
  • Merge branch command:
git merge 

Merge conflicts :
Merging is not just a simple operation of adding and removing files, Git will also merge modifications.

  • Delete branch command:
git branch -d (branchname)
# 例如
$ git branch -d testing
Deleted branch testing (was 85fc7e7).

Guess you like

Origin blog.csdn.net/qq_45934285/article/details/132599534