Git related notes

1 minute read

Published:

This is my personal notes for git.

Use both github and gitlab locally

Step 1

生成Github和GitLab的SSH

ssh-keygen -t rsa -C "注册 gitlab 账户的邮箱"
ssh-keygen -t rsa -C "注册 github 账户的邮箱"

当出现 “Enter file in which to save the key (/c/Users/luolingwei_sx/.ssh/id_rsa):”时,需自定义生成文件名 如:

Enter file in which to save the key (/c/Users/luolingwei_sx/.ssh/id_rsa): /c/Users/luolingwei_sx/.ssh/github_id_rsa #github_id_rsa为自定义的文件名

这个操作会给github生成单独的SSH文件github_id_rsa.pub

同样的操作生成gitlab的SSH文件gitlab_id_rsa.pub

Step 1 完成后,.ssh路径下会出现github_id_rsa.pub和gitlab_id_rsa.pub,将SSH内容分别粘贴到github和gitlab上即可

Step 2

编写config文件

由于本地调用私钥时默认使用 id_rsa,而我们是修改了名称的,所以要在.ssh路径下编写config文件,告诉本地调用哪个私钥。

创建config文件

touch ~/.ssh/config

配置config文件

Host github.com
HostName github.com
User Luolingwei
IdentityFile ~/.ssh/id_rsa

Host gitlab.qiyi.domain  #公司GitLab的域名
HostName gitlab.qiyi.domain  #公司GitLab的域名
User Luolingwei  #任意名称
IdentityFile ~/.ssh/gitlab_rsa

Step 3

验证配置是否成功

Github

ssh -T git@github.com

GitLab

因为每个公司的GitLab域名不同,所以不能用GitHub的测试方法,可以通过clone一个项目来测试

Git commands

frequently used commands:

  • git status (check status)
  • git add . (add all changes)
  • git add xxx/xxx (add specific files)
  • git commit -m “write comment here” (commit to local lib)
  • git psuh origin master / git push (push to remote lib)

set up name and email of git

  • git config user.name
  • git config user.email
  • git config –global user.name
  • git config –global user.email

cancel your local commit

  • git reset HEAD~<1/2/3> (cancel your x local commits)
  • git commit –amend (if you want to revise the comment of last commit)

cancel your remote push

  • git log -(x) (check last x versions)
  • git reset –hard < the first several numbers of version you want to roll back >
  • git push -f origin master (force push)

merge remote changes with your local changes

  • git stash
  • git pull –rebase
  • git stash pop