Home >Development Tools >git >An article explaining the installation and use of Git (with code)

An article explaining the installation and use of Git (with code)

奋力向前
奋力向前forward
2021-09-06 10:29:432677browse

In the previous article "WEB front-end cross-domain solutions you deserve to know (detailed code explanation)", we learned about WEB front-end cross-domain solutions. The following article will help you understand the installation and use of Git. Let's see how to do it together.

An article explaining the installation and use of Git (with code)

Git installation and use

$ yum -y install git  //安装git
$ git –-version  //验证

About git clone permissions

$ cd ~/.ssh
#如果没有密钥则不会有此文件夹,有则备份删除
#生成密钥:
$ ssh-keygen -t rsa -C "dev@chuchur.com"

# 按3个回车,密码为空。
# 最后得到了两个文件:id_rsa和id_rsa.pub

# 添加密钥到ssh:
$ ssh-add ~/.ssh/id_rsa (需要之前输入密码.)

# 将id_rsa.pub 内容  添加到gitlib or github
$ cat id_rsa.pub #得到内容

Some common commands about git

Rules take effect

#Git忽略规则和.gitignore规则不生效的解决办法
$ git rm -r --cached . ## 推荐做法,然后 git commit ,git push
#git rm --cached 删除的是追踪状态,而不是物理文件;如果你真的是彻底不想要了,你也可以直接 rm


#常见做法
$ git update-index --assume-unchanged <PATH> #告诉git ,别管我,就算我改变了,你当没看见,切换分支失效
$ git update-index --no-assume-unchanged <PATH> #告诉git ,来呀,快活啊.我改动了,
$ git update-index --skip-worktree <PATH> #告诉git ,从今以后别鸟我了, 死活与你无关.

Version management

#保存密码到本地,这样就不需要每次同步代码需要密码了, (慎重使用)
$ git config credential.helper store
$ git config --global user.name "username" #设置用户名
$ git config --global user.email "email"  #设置用户邮箱

#创建远程分支
$ git checkout -b dev #新建本地分支
$ git push origin dev:dev #push到远程分支

#删除分支
$ git branch -D dev #删除本地分支
$ git push origin :dev #删除远程分支 ,push一个空到分支到远程分支上
$ git push origin --delete dev #删除远程分支

#切换分支
$ git branch -a #查看分支列表,看不到最新分支,执行git pull
$ git checkout -b dev origin/dev #把远程的分支搞到本地并切换
$ git checkout dev # 切换到dev

#放弃清理本地更改
$ git checkout . && git clean -xdf

#缓存区操作
$ git add <file> | git add -A  | git add . #加入到缓存区
$ git commit -m &#39;功能开发完成&#39; #添加注释
$ git reset HEAD <file>  #缓存区退回工作区,没有commit之前,已经git add
$ git checkout -- <file> | git checkout . #丢弃工作区修改,没有git add

Version return

$ git reflog #查看commit 版本
$ git reset --soft <版本号> #切回版本,本地代码不会变,只是改变版本号
$ git reset --hard <版本号> #切回版本,本地代码会改变,版本号也会变,慎用
$ git reset --soft HEAD~1  #已经add,并且commit,不想push,想切回来接着改,可以这么干,直接切回上一个版本
$ git reset --soft HEAD^ #同上, 注意,仅仅是撤回commit操作,您写的代码仍然保留。
$ git rm --cached <文件名> | git clean -xdf # 丢弃本地或者其他
$ git push origin <分支> --force #加上--force 覆盖远程分支,因为退回版本之后本地版本比线上版本低,无法提交

--mixed: means: change the code without deleting the workspace, undo the commit, and undo git add. This operation is the default parameter, git reset --mixed HEAD^ and git reset HEAD^ have the same effect. --soft: Change the code without deleting the workspace, undo the commit, and do not undo git add. --hard:>Delete the workspace code change, undo the commit, and undo the git add. Note that after completing this operation, you will be restored to the last commit state.

Modify comments

If the commit comment is wrong and you just want to change it, just: git commit --amend, at this time You will enter the default vim editor, just save it after modifying the comments.

Gerrit related uses

# 拉代码
$ git pull
# 提交到master
$ git push origin HEAD:refs/for/master
# 提交到 dev-001
$ git push origin HEAD:refs/for/dev-001

There are some differences between gerrit and git, that is, every time you push the code, you must keep up with the code branch, otherwise the default is to submit to master

Recommended learning: Git video tutorial

The above is the detailed content of An article explaining the installation and use of Git (with code). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:chuchur.com. If there is any infringement, please contact admin@php.cn delete