Getting started with Git-from building a warehouse, pulling and pushing to branch management

Build a git repository

First, you need to have git installed on the system, whether you are linux or windows, after installation, cd to the directory where you need to build a git warehouse to execute:


git init | 当前目录作为Git仓库,初始化

git init --bare | 初始化一个裸仓库,仅用于记录git提交的历史信息,可以利用hook实现仓库和代码分离

The difference between the two can go to Baidu or refer to this article: git init and git init --bare

Warehouse and code separation reference: how to use git to update the online project code in real time

git commit code steps

*  git add .   | 提交新文件(new)和被修改(modified)文件,不包括被删除(deleted)文件
*  git add -u  | git add --update缩写 提交被修改(modified)和被删除(deleted)文件,不包括新文件(new)

1. git add -A  |  git add --all的缩写 提交所有变更,包含以上两种功能

2. git commit -m"备注"

3. git pull    | 拉取远端分支代码 (为了不影响远端代码建议先拉取再推送)

4. git push    | 当前分支只有一个远程分支,主机名都可以省略

git push format:

git push <远程主机名> <本地分支名>:<远程分支名>  | :前后必须没有空格的

git push origin master   |省略远程分支名,表示本地分支有存在"追踪关系"的远程分支(两个分支同名),如果该远程分支不存在,则会被新建;


* 删除远程仓库的分支 (省略本地分支推送会删除远程同名分支)
git push origin :master  |如果省略本地分支名,表示删除指定的远程分支,这等同于推送一个空的本地分支到远程分支。
* 等同于
git push origin --delete master

Git branch

An important functional branch of git, it can bring us a lot of convenience, each branch is equivalent to a version of the code base, convenient for multi-person collaboration.
When it comes to multiple branches, it is recommended to use git operation tools: sourcetree or tortoiseGit, etc., which is more convenient to deal with conflicts and other issues.
A master branch is assigned by default during git init. How to create a branch?

git branch tester		 |创建一个分支名为test的分支, 格式:git branch <branchname>
git branch dev 			 |创建一个分支名为dev的分支
git push origin dev:dev  | 新建的本地分支push到远程服务器(远程与本地同名),这样就创建了远程分支dev

View branch:

  git branch -r  	|查看远程分支名
  git branch  		|列出本地的分支
  git branch -a     |查看所有分支(本地和远程)

Switch branches:

 git checkout dev | 从当前分支切换到dev分支,格式: git checkout <branchname>

Delete branch

git branch -d tester | 删除 tester分支 

Branch merge

git checkout master |切换到master分支
git merge dev | 把dev分支合并到当前分支

For specific operation examples, please refer to Cainiao's tutorial: Git branch management

git rollback:

git revert -n 需要撤销的版本号  | 撤销某个版本的提交,并新建一个 “回退版本”
git commit -m"回退版本"
git push    		 		  | 将回退版本提交,这样就可以撤销指定版本的代码了

Here is an article about git rollback operations that is very clear:

Two ways to restore the previous version of Git: reset and revert

Git ignores commits.gitignore

In the process of using Git, some do not need to be submitted to the code repository, which requires the establishment of ignore rules to ignore the submission of these files.

Examples of rules:

/vendor  | 过滤整个文件夹
*.log    | 过滤所有.log 文件
/mtk/do.c | 过滤某个具体文件
!/mtk/one.txt | 追踪某个文件(不过滤)

Git matches the .gitignore configuration file by line from top to bottom.

If you perform the push operation before creating the .gitignore file, then even if you write the filter rule in the .gitignore file, the rule will not work, and git will still perform version management on the file. So you must first create a .gitignore file when deploying

Configuration syntax
Start with a slash "/" to indicate a directory;
use an asterisk "*" to wildly match multiple characters;
use a question mark "?"
to wildly match a single character and an exclamation mark "!" to indicate that the matched files or directories are not ignored (tracked) .

The .gitignore rule does not take effect. Solution
1: (Use with caution)
.gitignore can only ignore files that were not tracked. If some files have been included in version management, modifying .gitignore is invalid.
First delete the local cache (change to the untracked state), and then submit:

git rm -r --cached .
git add .
git commit -m 'update .gitignore'

Method 2: (recommended)
delete the local code, pull all the code again, and then fill in .gitignore (before get pull)

git clone clone

    git clone git://github.com/**/**.git     | 克隆格式 git clone <url>
    
    git clone git://github.com/*/project.git myproject | 克隆代码重命名为myproject 

Some other operations

git fetch origin master |将远程主机的最新内容拉到本地,不进行合并
git log | 列出历史提交记录
git log --oneline  | 查看历史记录的简版
git log --oneline --graph | 显示主分支和自分支git日志
git status |  查看当前git状态信息(查看是否有文件未提交)
git remote | 查看本地添加了哪些远程分支地址
git remote add origin https://xxxxxxxxxxxx.git | 添加远程地址
git branch -m <oldbranch> <newbranch>          | 重命名本地分支

git gc        | 清理不必要的文件并优化本地存储库

Guess you like

Origin blog.csdn.net/qq_39004843/article/details/106069368