Git common operations and commands

Git common operations and commands

Local library operations

View git command help document

  • After executing this command will pop up a browser, the browser displays help files, because the machine performance reasons, open the document, the command will be executed after a slight delay to wait
  • html page displayed in the browser is a local static pages
#init是查看init命令的帮助文档
git help init

Create or initialize the local library

  • If this path has been the local library, then restored to the original state
  • This command creates a hidden folder git, do not delete files in the folder in the path, and do not arbitrarily change
#创建、初始化该路径
git init    
#在该路径下创建demo文件夹,并将其创建为本地库。若该文件夹存在,则将其初始化
git init demo

Delete the local library

  • Delete the local library just need to delete hidden files in the local git repository folder to
  • If the project files are not retained, the directory can delete items

    Signature settings

  • The purpose is to distinguish the identity of different developers, requires the user name and mailbox, signatures and signature user-level system-level signature project, the two must set one up.
  • The results at the project level signature settings will be configured into .git / config file, .gitconfig file in the user-level configuration system configures the user into the system root directory
  • Here set the user name and mailbox has nothing to do with GitHub account, the mailbox will not set a mailbox to send mail, just to identify the identity
#项目级别:仅在本地库范围有效,优先级高
git config user.name jerry
git config user.email [email protected]
#系统用户级别(操作系统登录的用户):在当前操作系统的所有本地库有效
git config --global user.name jerry
git config --global user.email [email protected]

View project status

  • If there is a new file is added or modified, use this command, these files will be highlighted
  • The command also displays the status of the staging area and local libraries
git status

Add files to the staging area

  • File staging area records to be submitted, not submitted documents will not be ready to be added to the staging area
  • File only after submitting files to the staging area, in order to submit to the local library, and the library is to be submitted to the local staging area file, not a workspace file
#添加所有变更文件及子目录下的文件至暂存区
git add .
#添加单个变更文件至暂存区
git add demo.txt
#添加子目录下的变更文件至暂存区
git add com/app.txt

Undo file from scratch

git rm --cached demo.txt

Submitted to the staging area to the local library

  • If the file is modified, you can not git adddirectly use this command to submit, but if it is a new file, you must git addthen use the command
  • Can be oriented sub-set of remarks submitted information to submit documents to the local library, this operation is not required
#将暂存区的某个文件提交至本地库。使用该命令成功后,会进入vim编辑页面,这个页面中可添加备注消息。操作vim与linux操作vim的方式一致
git commit demo.txt
#可以使用 -m 参数,直接添加备注消息
git commit -m "git demo" demo.txt 
#将暂存区的所有文件提交至本地库
git commit -m "commit all"

View Project Operation Log

#显示完整的日志信息
git log
#以一行形式显示日志信息
git log --pretty=oneline
#以一行形式显示日志信息,且简化索引
git log --oneline
#显示完整的日志信息,且显示指针与步长
git reflog

Project version rollback

  • Rollback version refers to the restoration project workspace file to a temporary area version of the state
  • reset commands commonly used parameters:
    --soft: change only the local library version will not change the work area and staging area version
    --mixed: change only the local library and the staging area version, does not change the workspace version
    --hard: the local library, staging area and workspace version changed
  • HEAD demonstrated here is a pointer to the master branch
#根据版本索引移动版本,可前进可后退,使用后,log依然包含所有版本
git reset --hard 7c213d1
#使用散列值移动版本,可前进可后退,使用后,log依然包含所有版本
git reset --hard HEAD@{1}
#使用^移动版本,只能后退不能前进,HEAD后有几个^就移动几步,使用后,log只显示目前版本和之前版本
git reset --hard HEAD^
#使用~移动版本,只能后退不能前进,~后是数字几就移动几步,使用后,log只显示目前版本和之前版本
git reset --hard HEAD~1

Comparison of file differences

  • The default is in contrast to the temporary area version
  • Can be compared with historical versions of your local library
  • Command with a file name is the difference in contrast to a single file, without a file name is the file Compare all the difference in the current workspace
#将当前工作区的demo.text与暂存区demo.text对比差异
git diff demo.text
#将当前工作区的demo.text与上一个历史版本比较差异
git diff HEAD^ demo.text
#将当前工作区的所有文件与暂存区对比差异
git diff
#将当前工作区的所有文件与最新历史版本比较差异
git diff HEAD

Branch operation

View all branches of the current project

git branch -v

Creating a branch

#创建一个名为new_branch的分支
git branch new_branch

Switching branch

#切换到new_branch分支工作
git checkout new_branch

Deleted branches

#删除不是当前所在分支的分支
git branch -d new_branch

Recovery branch

#通过散列值恢复分支
git branch new_branch HEAD@{1}
#通过^恢复分支
git branch new_branch HEAD^
#其他恢复方式与回滚项目版本中方式一致

Merge branch

  • The combined branch before switching to the branch need to be merged at the branch.
  • Example: To new_branch branch into the master branch, it is necessary to pass git checkoutoperating switches back to the master branch
#将new_branch这个分支合并到当前分支
git merge new_branch

Merge branch conflict resolution

  • The reason of conflict: the same version of the changes to the same file at the same there are two branches
  • When the performance of a conflict git: command-line display (branch name | MERGING), represented in the combined state, has generated conflict. View the conflict files, git will add marked as part of the conflict
  • Conflict resolution, conflict must first manually modify the contents of the file, select where to reservations where discarded, and then do the following:
#将冲突状态标记为已解决
git add demo.text
#退出合并状态,也可通过 -m 参数添加注释消息,切记不能带文件名,本次操作是退出合并状态而不是将文件提交到本地库
git commit

Remote library operations

Create an alias for the remote database address

#test是别名,https://github.com/rawlins2397/test.git是远程仓库地址
git remote add test https://github.com/rawlins2397/test.git

View all aliases created

git remote -v

Rename the alias

#将已有别名test重命名为test1
git remote rename test test1

Delete an alias

#删除已有别名test
git remote remove test

Push local library to the remote library

  • The command will be asked to enter the remote database account and password to authenticate execution
  • The order involves file upload operation, due to network reasons, we need to wait uploaded
  • If pushed through an alias, the alias cloning will also clone
#需要指定远程库地址和要推送的分支
git push https://github.com/rawlins2397/test.git master
#远程库地址可是使用别名代替,这里的test是别名
git push test master

Cloned the remote repository to the local library

  • Clone operation will complete remote local library clones to complete cloning operation means for SHA-1 hash encryption algorithm to ensure that the check item without data loss in the network transfer
  • Local directory does not need to git initbe initialized to the local library, cloning success will automatically initialized
  • If the pusher is trying to push through a local library alias, then clone the configuration also creates an alias
git clone https://github.com/rawlins2397/test.git

Pull the remote repository to the local library

  • Different pull remote library and cloning is pulling representing the local existing local library for the project, but pulled the latest version of the file to merge
  • Pulling two ways: directly git pulland use step git fetchandgit merge
  • git pullCommand git fetchand git mergebinding
#该操作将远程库的分支拉取到本地库的一个分支,并没有进行合并,test是远程库别名,master是要拉取的远程库的分支,这个分支拉取成功后可通过test/master来使用
git fetch test master
#将fetch拉取的分支合并到当前分支
git merge test/master
#通过pull可以直接将别名为test的远程库master分支拉取并合并到本地当前分支
git pull test master

Push with conflict resolution version

  • In the multi-member development process, modifications to the same version of the same file, giving priority to push staff will push successful, you will be prompted to pull and push them up when people push after push, which is pushing the conflict with the version of the performance
  • To resolve the conflict with the version of the push, the first remote repository latest version pulled to the local, due to a conflict of pulling the branch and the local branch of the merger, the merger may refer branch conflict resolution to resolve the conflict

    SSH public key generation

  • SSH public key local library can be achieved with the remote library of free secret secure transmission
  • A case of only one operating system account an SSH public key, and therefore by remote SSH library binding only applies to users generally operate only one operating system account and a remote library account
#切换到用户根目录,查看是否存在something 和 something.pub 来命名的一对文件,这个 something 通常就是 id_dsa 或 id_rsa,如果有,打开.pub文件,其内容就是SSH公钥
cd ~/.ssh 
ls
#若不存在,则需要创建一个SSH公钥
ssh-keygen -t rsa -C "[email protected]"
#ssh-keygen的参数为:-t 指定密钥类型,默认是 rsa ,可以省略。-C 设置注释文字,通常为邮,注意C必须为大写。-f 指定SSH存储的文件名。
#运行命令后,若没有使用-f参数,则会提示输入文件名,可回车使用默认值。还会要求输入两次密码,这个密码是用于使用SSH公钥的密码,而非登录远程库帐号的密码,若不需要,直接回车忽略
#.pub文件,其内容就是SSH公钥

Ignore files pushed to the remote repository

  • Projects often have configuration files generated code editor, these profiles nothing to do with the project itself, so when the project will be pushed to the remote repository, you need to set the push to ignore these files
  • Code editor generated profile if you do not ignore the push, will jeopardize project development, because these configuration files are associated with a version of the code editor can not guarantee that a project can only use the same version of the editor, so do not ignore the will have unnecessary merge conflicts
  • Go https://github.com/github/gitignore to see ignored files related to language project
  • Preparation of documents, the contents of this document refer to the above URL, and can also be custom file types to ignore, and (if only need to set up a local library to ignore ~ ​​/ .gitconfig file, the local library .git / config configuration) introducing said document
[core] 
    excludesfile = C:/Users/rawlins/Java.gitignore

Guess you like

Origin www.cnblogs.com/rawlins/p/11681674.html