The use of Git and Github [personal records kept updated]

00 About tool configuration

01 Git and GitHub configuration (Ubuntu)

  1. Check Gitthe installation

    # Git的安装
    sudo apt install git
    
    # 检查
    dargon@dd:~/桌面$ git version
    git version 2.25.1
    
  2. GitAuthorization is performed githubthrough the generated public key and secret key. If you want to connect locally with the warehouse on the Internet, you need to configure the connection between the two separately.sshgithub

  3. # 生成SSH key
    dargon@dd:~$ ssh-keygen -t rsa
    Generating public/private rsa key pair.
    Enter file in which to save the key (/home/dargon/.ssh/id_rsa): 
    Enter passphrase (empty for no passphrase): 
    Enter same passphrase again: 
    
  4. id_rsa.pubAdd the content and assignment of the generated public key to Githubthe above, so that the local secret key can githubbe matched with the above public key for authorization

  5. Verify the connection was successful

    dargon@dd:~$ ssh -T [email protected]
    Hi Dargon0123! You've successfully authenticated, but GitHub does not provide shell access.
    
  6. The above completes the binding Gitwithgithub

01 Commonly used commands in local warehouse

  1. git init 
    # 本地初始化一个文件夹,为gii仓库,初始化完成之后,文件夹中会多出.git 文件
    
  2. git status
    # 显示仓库的状态信息,查看仓库的状态
    
  3. git add
    # 将本地仓库的新开发的文件,通过add 命令添加搭到,[仓库缓冲区]的地方,此时还未提交到仓库,等待commit
    git add . 
    # 针对该本地仓库中,多个新开发的文件,进行一次性添加到 缓冲区
    
  4. git commit -m "text message"
    # 正式将[仓库缓冲区]的文件,提交到该本地仓库,等待push 到远端仓库,即是github 上面
    
  5. git log
    # 打印出 仓库的提交日志,是一个完整的提交记录,包括 author date and commit message
    
    git log -1 HEAD
    # 打印出上次commit的一个完整记录,包括author,date,commit message
    
    git log --oneline
    # 打印上次一条 commit message
    
  6. git branch
    # 查看当前仓库的分支,*master 意味着当前所在的分支
    
    git branch -a
    # 查看当前仓库中所有以及隐藏的分支
    
    git branch a 
    # 新建分支a 
    
    git checkout -b b
    # 新建分支 b 并且切换到分支b 
    
  7. git checkout [分支名]
    # git checkout a ; 切换到分支a
    # git checkout master ; 切换到分支master
    
  8. git merge [分支名]
    # 将分支切换到 master
    # git merge a ; 合并a 分支到master
    # note: 合并的两个分支内容不能有冲突,否则合并不成功
    
  9. git branch -d [分支名]
    # 删除该分支
    
  10. git tag [标签名]
    # git tag v1.0 ; 给当前分支 add 标签名
    # 方便查找 git checkout v1.0 就相当于 git checkout [该分支]
    
  11. gitk --all &
    # 查看当前的仓库的关系
    

02 Interact with the remote warehouse

  1. git push origin master
    # 将本地代码仓库推到远程仓库,保持两者同步
    
  2. git pull origin master
    # 将远程仓库代码拉到本地,保持同步
    
  3. There is no local warehouse, first clone a warehouse, the following is the development on the local warehouse, then add, commit, and finally push to the remote warehouse

    git clone [Github仓库地址]
    # 针对本地没有仓库,将该仓库克隆到本地
    
  4. There is a warehouse locally, initialize first, and then associate with the remote warehouse , originwhich is the name of the default remote warehouse, develop, submit, and finally push

    git remote add origin [仓库地址]
    # 关联远程仓库,同步本地与远程的信息
    
    git pull origin master
    # 同步两个仓库的代码
    
  5. To update/delete files in the warehouse, first pull the remote warehouse over, then delete the corresponding files locally, and then add, commit,pushend the operation.

  6. Delete a branch in the remote warehouse

    git push origin :[分支名]
    # 直接删除远程仓库中该分支
    

03 Merge Githubthe two branches

  1. Generally, when executing git clonecommands locally, the default is the branch cloneof the remote warehouse master. When merging branches, another branch is needed. How to pull the content of another branch locally

    # 01 显示所有的branch
    $ git branch -a
    * master
      remotes/origin/main
      remotes/origin/master
    
    # 02 目录切换到分支main的内容 
    git checkout origin/main 
    
    $ git branch -a
    * (HEAD detached at origin/main)
      master
      remotes/origin/main
      remotes/origin/master
    
    # 03 查看成功切换到main 这里
    git checkout main
    $ git branch
    * main
      master
      
    $ git branch -a
    * main
      master
      remotes/origin/main
      remotes/origin/master
    
    
    # 04 merge
    # 先切换到 branch master
    # occur error
    $ git merge main
    fatal: refusing to merge unrelated histories
    # solve error
    $ git merge main --allow-unrelated-histories
    Merge made by the 'recursive' strategy.
     hit.txt | 1 +
     1 file changed, 1 insertion(+)
     create mode 100644 hit.txt
    # merge successful
    
    $ git push origin master
    # push 到远程仓库
    

02 About the use of Git tools

These are common Git commands used in various situations:

start a working area (see also: git help tutorial)
   clone             Clone a repository into a new directory
   init              Create an empty Git repository or reinitialize an existing one

work on the current change (see also: git help everyday)
   add               Add file contents to the index
   mv                Move or rename a file, a directory, or a symlink
   restore           Restore working tree files
   rm                Remove files from the working tree and from the index
   sparse-checkout   Initialize and modify the sparse-checkout

examine the history and state (see also: git help revisions)
   bisect            Use binary search to find the commit that introduced a bug
   diff              Show changes between commits, commit and working tree, etc
   grep              Print lines matching a pattern
   log               Show commit logs
   show              Show various types of objects
   status            Show the working tree status

grow, mark and tweak your common history
   branch            List, create, or delete branches
   commit            Record changes to the repository
   merge             Join two or more development histories together
   rebase            Reapply commits on top of another base tip
   reset             Reset current HEAD to the specified state
   switch            Switch branches
   tag               Create, list, delete or verify a tag object signed with GPG

collaborate (see also: git help workflows)
   fetch             Download objects and refs from another repository
   pull              Fetch from and integrate with another repository or a local branch
   push              Update remote refs along with associated objects

workflows)
fetch Download objects and refs from another repository
pull Fetch from and integrate with another repository or a local branch
push Update remote refs along with associated objects


Guess you like

Origin blog.csdn.net/Dallas01/article/details/123030732