Git tutorial (Li Lichao git and GitHub use)

Git Tutorial

configuration

  • Configure name and email

git config --global user.name "xxxx"
git config --global user.email "[email protected]"

git status #查看仓库状态
git init #初始化仓库
git add #将文件切换到暂存的状态,未跟踪===》暂存
git add * #将所有的已修改,未跟踪的文件暂存
git commit -m "修改了什么,完成了什么功能" #将暂存的文件存储到仓库中 暂存===》未修改
git commit -a -m "xxx" #提交所有已修改的文件(未跟踪的文件不会提交)
  •  Reset files, delete files, move files
①重置文件
git restore <filename>      #恢复文件,后悔操作时,进行恢复到从前的代码
git restore --staged <filename>             #取消暂存状态
上面这个命令不会取消你的删除操作,此时文件还是不存在的,执行git restore fileName恢复文件,此时文件又存在了。
②删除文件
git rm <filename>                           #删除文件
git rm <filename> -f                        #强制删除
③移动文件
git mv from to                          #移动文件,重命名
git log                                 #查看提交信息  

the branch

Git will create a node every time the code is submitted

git branch <分支名>                    #创建分支
git branch -d <分支名>                 #删除分支
git branch                            #查看分支
git switch <分支名>                    #切换分支名
#安装GitLens插件,可以在vscode中的COMMIT DETAILS中查看提交graph
git switch -c <分支名>                 #创建并切换分支
git merge <合并的分支名>                #合并分支

Note: vscode + temporary storage, commit submission

In the actual development process, the first thing we need to do is to git switch -c <分支名>create a branch, write code on our own branch, so that each branch does not affect each other, even if the code we write is bad, it will not affect the code on the main branch . When we have implemented some functions, we need to make our code available on the main branch. At this time, what we need to do is to merge our code with the main branch.


Merge method 1: Fast-Forward (the branch and the main branch are on the same line)

  • First git switch masterswitch to the master branch
  • Execute on the master branchgit merge <想要合并的分支名>
  • After merging, the merged branch is useless at this time (for example, bug1 branch: represents the operation of modifying bug1), and the git branch -d bug1branch bug1 is deleted.

The second way of merging: Fast-Forward (the branch and the main branch are not on the same line)

  • The operation at this time is similar to method 1, except that we need to merge manually in the end.

Rebase rebase

Need to go back to the rebased branch, execute

git rebase master

Then go back to the git switch master main branch, and execute git merge <branch name> to complete the rebase merge.

Upload the local library to github:

git remote add origin https://github.com/lilichao/git-demo.git
# git remote add <remote name> <url>

git branch -M main
# 修改分支的名字的为main
git push -u origin main
# git push 将代码上传服务器上

Remote library operation command

git remote #列出当前关联的远程库 
git remote add <远程库名> <url> #关联远程仓库
git remote remove <远程库名>
git push -u <远程库名> <分支名> # 像远程仓库推送代码,并和当前分支关联
git push <远程库> <本地分支>:<远程分支>
git push # 如果本地的版本低于远程库,push默认是推不上去
git fetch #拉取远程仓库的代码,但是之后必须执行合并操作merge,解决冲突之后,在重新git add、git commit、git push 等操作
git fetch # 要想推送成功,必须先确保本地库和远程库的版本一致,fetch它会从远程仓库下载所有代码,但是它不会将代码和当前分支自动合并
         # 使用fetch拉取代码后,必须要手动对代码进行合并    
git pull  # 从服务器上拉取代码并自动合并 

Note: Before pushing, be sure to pull the latest code in the warehouse

label tag

If you have to go back to the later node to operate the code, you can choose to create a branch and then operate

git switch -c <分支名> <提交id>

You can set labels for the submission records. After setting the labels, you can quickly identify different development nodes through the labels:

git tag
git tag 版本
git tag 版本 提交id
git push 远程仓库 标签名 #指定的标签推送到远程仓库
git push 远程仓库 --tags #将所有的标签推送到远程仓库
git tag -d 标签名 # 删除标签
git push 远程仓库 --delete 标签名 # 删除远程标签

.gitignore

By default, git will monitor all content in the project, but some content, such as the content in the node_modules directory, we don't want it to be managed by git. We can add a file in the project directory .gitignoreto set those files that need to be ignored by git. Write files that need to be ignored directly in the file.

github's static page

  • In github, you can deploy your own static pages to github, which will provide us with an address to make our page a real website, which can be accessed by users.

  • Require:

    • The branch for static pages must be called: gh-pages (git switch -c ph-pages)

    • If you want the page to be accessible through xxx.github.io, you need to configure the warehouse name as xxx.github.io git branch -M xxx.github.io

Guess you like

Origin blog.csdn.net/qq_44808827/article/details/128815959