Git Advanced Road (2) - Practical Operation

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/weixin_45505313/article/details/102633900

1.git config configuration information

git setup file .gitconfig, it can (global configuration) in the user's home directory, you can also (project configuration) in the project directory

# 显示当前的Git配置
$ git config --list

# 编辑Git配置文件
$ git config -e [--global]

# 设置提交代码时的用户信息
$ git config [--global] user.name "[name]"
$ git config [--global] user.email "[email address]"

Also due to the remote server interaction, pull 和 pushthe code, when the need to generate a new git installed ssh keyas an interactive document, the following command which

# 该命令通常会生成 ~/.ssh 目录,其中后缀为 .pub 的文件即为公钥,需将其添加到远程服务器的 ssh key 中
$ ssh-keygen

2. git clean clear workspace modifications

This command is used to clean up in their own workspace changes (red), such as the code is changed mess and start again want to clean out all the modifications

//清理所有workspace下的改动
git clean -f -d

//清理所有workspace下的改动,包含忽视的文件
git clean -f -d -x

//指定清理的路径,可以很方便的去掉指定的一个目录或多个文件的新改动
git clean -f -d <Path>

3. git format-patch to generate patch files

This patch file save command to commit the particular format, with git am patch can then take these come in, which is generally the following steps

  1. The oldest commit1 to submit between the newer commit generated patch file

    $ git format-patch commit1^..commit2
    
  2. You can use git apply the patch into the branch, but does not commit

    git apply xxx.patch
    
  3. Git am using the patch into the branch, and directly to, cherry-pick corresponding to a plurality of disposable Submit

    # 打入单笔提交
    git am xxx.patch
    
    #一次性打入多笔提交
    git am *.patch
    

4. git push to create / delete remote branch

command Features
git push < remote host > <Local branch or HEAD>: < Remote branch > The native code is pushed to the remote branch
git push origin master Local changes will be submitted to the UPSTREAM REPOSITORY
git push [email protected]:username/project.git Local changes will be submitted to the designated UPSTREAM REPOSITORY
git push origin master -f Forced local changes will be submitted to the UPSTREAM REPOSITORY, if there is a conflict, it will be forced to cover

Branched push sequence is written <Origin>: <dest>, it is git pull <remote branch>: <local branch>, and git push the <local branch>: <remote branch>. If you omit the remote branch name, it said it would push the local branch with the presence of "trace relationship" remote branch (usually both of the same name), if the remote branch does not exist, it will be the new

  1. Use the following command to create a new local branch dev, dev this time is a local branch of a remote repository does not know it's there
    # 创建并切换到本地分支 dev
    git checkout -b dev
    
    # 以下命令可以删除本地分支
    git checkout master # 切换到master分支
    git branch -d dev # 删除本地dev分支
    
    # 查看所有分支,当前所在分支会被星号(*)标注
    git branch -a 
    
  2. Dev branch will be posted to the remote repository, which is synchronized dev branch code to the remote server
    # 创建远程仓库 dev 分支
    git push origin dev
    
    # 删除远程仓库dev分支,注意origin后面的空格
    git push origin :dev 
    

5. git commit --amend modify the remote branch history submitted

5.1 modify a submission on a remote branch

  1. Use git in the process, sometimes we will find the time to develop a feature or a bug fix of incomplete commit, that is, did not meet expectations. This time to modify the code, re-raise a sum commit although to achieve the purpose, but also the submission of integrity commit destroyed.At this point we can locally modify the code, then use the following command to commit the time to merge the temporary modifications and on, with the submission of a new commit replacement. No run-time file cache This command can be used to edit the last report to the information submitted, without changing the content commit
     //将本地修改使用 git add 添加到暂存区后,使用以下命令覆盖上一次提交
     git commit --amend
    
     //将新的提交 push 到远程分支,注意 -f 参数,没有会导致远程与本地的一次merge操作,产生冗余提交记录
     git push -f
    
  2. Information on the time of submission of modification
    git commit --amend --author="userName <userMail>"
    git push --force-with-lease origin master
    

5.2 modify the remote branch to submit any information

The situation wrong message may occur when using git submit code, but this time commit has been push to a remote server, if you want to modify the main required to the following command. Also note,When you modify history commit message, make sure the current branch is the latest code, and have submitted all local modifications

 git log
 git rebase -i HEAD~n # n指定距离最新提交的几笔提交
 git commit --amend
 git rebase --continue
 git push -f
  1. git log View History
    $ git log
    

Here Insert Picture Description

  1. git rebase -i HEAD~3To determine which modify commit
    wherein HEAD ~ 3 indicates the most recent three submitted, in the present embodiment only relates to the most recent pen 3 and the second pen submitted modifications, as shown below
    $ git rebase -i HEAD~3
    

Here Insert Picture Description

  1. Use git commit --amendand git rebase --continuemodify the Edit commit
    step performed after the completion of the first HEAD pointer points to a need to modify the commit, then you can use the following command to commit changes to complete the Message
    # 进入vim 编辑模式,修改 commit message
    $ git commit --amend
    
    # 完成一个commit 的修改,HEAD 指向下一个需要修改的commit
    # 如果全部修改完毕,则提示 Successfully rebased and updated refs/heads/dev.
    $ git rebase --continue
    

Here Insert Picture Description

  1. git logView Submitted history, git push -fwill modify the update to the remote server
    $ git push -f
    

Guess you like

Origin blog.csdn.net/weixin_45505313/article/details/102633900