An article explaining the installation and use of Git (with code)
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.
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 '功能开发完成' #添加注释 $ 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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

To download projects locally via Git, follow these steps: Install Git. Navigate to the project directory. cloning the remote repository using the following command: git clone https://github.com/username/repository-name.git

Steps to update git code: Check out code: git clone https://github.com/username/repo.git Get the latest changes: git fetch merge changes: git merge origin/master push changes (optional): git push origin master

Git Commit is a command that records file changes to a Git repository to save a snapshot of the current state of the project. How to use it is as follows: Add changes to the temporary storage area Write a concise and informative submission message to save and exit the submission message to complete the submission optionally: Add a signature for the submission Use git log to view the submission content

Git code merge process: Pull the latest changes to avoid conflicts. Switch to the branch you want to merge. Initiate a merge, specifying the branch to merge. Resolve merge conflicts (if any). Staging and commit merge, providing commit message.

Resolve: When Git download speed is slow, you can take the following steps: Check the network connection and try to switch the connection method. Optimize Git configuration: Increase the POST buffer size (git config --global http.postBuffer 524288000), and reduce the low-speed limit (git config --global http.lowSpeedLimit 1000). Use a Git proxy (such as git-proxy or git-lfs-proxy). Try using a different Git client (such as Sourcetree or Github Desktop). Check for fire protection

To delete a Git repository, follow these steps: Confirm the repository you want to delete. Local deletion of repository: Use the rm -rf command to delete its folder. Remotely delete a warehouse: Navigate to the warehouse settings, find the "Delete Warehouse" option, and confirm the operation.

How to update local Git code? Use git fetch to pull the latest changes from the remote repository. Merge remote changes to the local branch using git merge origin/<remote branch name>. Resolve conflicts arising from mergers. Use git commit -m "Merge branch <Remote branch name>" to submit merge changes and apply updates.

When developing an e-commerce website, I encountered a difficult problem: How to achieve efficient search functions in large amounts of product data? Traditional database searches are inefficient and have poor user experience. After some research, I discovered the search engine Typesense and solved this problem through its official PHP client typesense/typesense-php, which greatly improved the search performance.
