Table of contents
2.1 Push the local project to the remote warehouse of github
2.2 Local modification has added many files, these are not needed, how to discard them
2.3 Forcibly pull the latest code of the remote branch and overwrite the local branch
2.4 View the difference between local branch and remote branch
1. Git basics
1.1 What is git
Git is a distributed source code management tool (version control tool). It is currently the most advanced and popular version control system in the world. It can quickly and efficiently handle project version management from very small to very large.
Features: The larger and more complex the project, the more collaborative developers, the more it can reflect the high performance and high availability of Git.
For a better understanding, refer to: What exactly is Git? bazyd
1.2 git installation
1) Download from the official website, suitable for your own version of git
Official website address: https://git-scm.com
2) Check whether git is installed successfully
Open the terminal and enter the command
git --version
The git version you installed appears, even if it is successfully installed!
1.3 Common commands of git
1.3.1 git checkout
git checkout . //本地所有修改的。没有的提交的,都返回到原来的状态
1.3.5 git clean
The git clean parameter
-n does not actually delete, but just performs a drill to show what will be done and which files will be deleted. (You can use the command parameters first, and then decide whether to execute)
-f delete files
-i display the files to be deleted
-d recursively delete directories and files (untracked)
-q only display errors, successfully deleted files are not displayed
Second, the use of git
2.1 Push the local project to the remote warehouse of github
1) Ensure that it is in the directory that needs to be submitted, and the initialization warehouse is submitted locally
git init
Here's what the yellow warning hint says: Choose to use 'master' as the initial branch name. Default branch name hint: subject to change. Configure the initial branch name to use in all branches HINT: Your new repository, to get this warning gone, call: git config --global init.defaultBranch <name>
This warning is harmless, just ignore it. According to the prompt in the last line, it can be seen that the warehouse has been initialized
2) Add the files in the current directory to the warehouse (buffer)
//add后面的.指的是将当前文件夹下所有变动都添加进去
git add .
Submit to local repository
//init是自己写的,可以随便改,但是尽量是英文
git commit -m "init"
3) Associate the local warehouse with the remote and push it to the remote warehouse
Log in to github to create a new warehouse, as follows
click new
After the warehouse is created, github will also give a very detailed command line guide, as shown in the red box in the figure below
Go back to the local command line and enter the following command
//与远程仓库关联
git remote add origin 远程仓库地址
//例如: git remote add origin [email protected]:yanhanrebecca/c_learn.git
//推送到远程仓库
git push -u origin master
The local display has been successful,
Log in to github, refresh the interface, we can see that the submission has been successful
2.2 Local modification has added many files, these are not needed, how to discard them
1)查看要删除的文件及目录,确认无误后再使用下面的命令进行删除
git clean -nxdf
2) All local modifications and no submissions will return to the original state && deleted files and directories
git checkout . && git clean -xdf
2.3 Forcibly pull the latest code of the remote branch and overwrite the local branch
Use the following command:
git fetch --all
git reset --hard origin/<remote_branch_name>
2.4 View the difference between local branch and remote branch
git fetch: It is equivalent to fetching the latest version from remote to local, without automatic merge (recommended)
git fetch origin master // 从远程的origin的master主分支下载最新的版本到origin/master分支上
git log -p master..origin/master // 比较本地的master分支和origin/master分支的差别,-p用于显示每个提交的详细差异信息
git merge origin/master // 进行合并
// 上述过程其实可以用以下更清晰的方式来进行:
git fetch origin master:tmp
git diff tmp
git merge tmp