密码相关

  1. 生成sshkey
    在终端中敲下面的命令,第一步会生成一对私钥和公钥,第二步查看公钥字符串。

    1. ssh-keygen -t rsa -C "$your_email" (这里要替换成自己的邮箱)
    2. cat ~/.ssh/id_rsa.pub
  2. 保存sshkey到gitlab
    在面板上依次点击Profile Settings –> SSH Keys –> Add SSH Keys。然后把上一步中的id_rsa.pub中的内容拷贝出来粘贴到输入框中,保存。

  3. git push的时候每次都要输入用户名和密码的问题解决
    原因:在添加远程库的时候使用了https的方式,所以每次都要用https的方式push到远程库
    测试:查看使用的传输协议

    1. git remote -v
    2. origin https://github.com/toyijiu/DailyBlog.git (fetch)
    3. origin https://github.com/toyijiu/DailyBlog.git (push)

对策:重新设置成ssh的方式

  1. git remote rm origin
  2. git remote add origin git@github.com:username/repository.git (这里要替换成自己的路径)
  3. git push -u origin master

确认:再看下当前的传输协议

  1. git remote -v
  2. origin git@github.com:toyijiu/DailyBlog.git (fetch)
  3. origin git@github.com:toyijiu/DailyBlog.git (push)

上传相关

  1. 告诉服务器你是谁

    1. git config --global user.email "you@example.com" (这里要替换成自己的邮箱)
    2. git config --global user.name "Your Name" (这里要替换成自己的用户名)
  2. 上传初始代码

    1. cd project_root (这里要替换成自己的文件夹)
    2. git init (初始化git仓库)
    3. git add . (添加文件到暂存区)
    4. git commit -m 'init commit' (提交代码到仓库)
    5. git remote add origin git@example.com:namespace/projectname.git (链接到git server,这里要替换成自己的仓库地址)
    6. git push --set-upstream origin master (push代码到服务器)
  3. 上传修改代码

    1. git add -A (把所有修改的代码放到暂存区)
    2. git add src/XXX (把要提交的代码放到暂存区)
    3. git commit -m "说明内容" (上传代码)
    4. git push (将本地分支的更新,推送到远程)

Push an existing folder

  1. cd existing_folder
  2. git init
  3. git remote add origin git@gitlab.com:chenys/response-dev.git
  4. git add .
  5. git commit -m "Initial commit"
  6. git push -u origin master

Push an existing Git repository

  1. cd existing_git_repo
  2. git remote rm origin
  3. git remote add origin git@gitlab.jiahenghealth.com:ef_erp/erpman-front.git
  4. git push -u origin master
  1. 修改最近一次commit履历
    1. git commit --amend

解决仓库迁移问题
https://blog.csdn.net/qq_39400546/article/details/100150320

下载相关

  1. 从现有仓库克隆

    1. git clone git@gitlab.com:chenys/elevator-erp.git (这里要替换成自己的代码仓库地址)
  2. 取得最新版本

    1. git pull

取消相关

  1. git checkout xxx (把本地修改后的代码返回回来,xxx是文件名)
  2. git checkout [commitsha值] 回滚到sha值版本
  3. // 未使用git add 缓存代码时
  4. git checkout . (撤销所有)
  5. // 已经使用了git add 缓存了代码。
  6. git reset HEAD . (撤销所有)
  7. // 已经用git commit 提交了代码
  8. git reset [commitsha值] 回滚到sha值版本,之后的修改会保存到本地
  1. $ git log
  2. commit 35701bd347d8c96505b81daa28512ce489238fb4 (HEAD -> dev, origin/dev)
  3. // 35701bd347d8c96505b81daa28512ce489238fb4 就是commit的sha值

分支相关

  1. git brandh (查看当前分支)
  2. git branch chenys / git checkout -b chenys (创建代码分支chenys)
  3. git branch -d chenys(删除代码分支chenys
  4. git checkout chenys (切换到分支chenys)
  5. git merge master (把master上的代码合并到当前分支)
  6. git branch -m oldBranchName newBranchName

cherry-pick
把某分支(比如develop分支)的某次提交合并到另一分支(比如master分支),就需要用到git cherry-pick命令。

  1. 切换到develop分支
  2. 敲 git log 命令,查找需要合并的commit记录,比如commitID:24263e2c
  3. 切换到master分支,使用 git cherry-pick 24263e2c 命令,把该条commit记录合并到了master分支,这只是在本地合并到了master分支;
  4. git push 提交到master远程

查询相关

  1. git status (查看代码的修改状况)
  2. git diff (查看已修改的内容)
  3. git log (查看各种信息,commit信息,分支信息等)

查询某个文件的修改履历

  1. cd src/XXX (切换到文件所在目录,XXX代表文件所在的文件夹)
  2. git log -p XXX (显示该文件的所有修改履历,XXX代表文件名)

别名相关

  1. 查看别名

    1. git config --list|grep alias
  2. 设置别名

    1. git config --global alias.co checkout
    2. git config --global alias.br branch
    3. git config --global alias.ci commit
    4. git config --global alias.st status

Tag相关

  1. git tag 查看所有tag
  2. git tag v1.0 tag(v1.0)
  3. git push origin v1.0 tag推到远端
  1. 💥 feat(compiler): add 'comments' option
  2. 🐛 fix(compiler): fix some bug
  3. 📝 docs(compiler): add some docs
  4. 🌷 UI(compiler): better styles
  5. 🏰 chore(compiler): Made some changes to the scaffolding
  6. 🌐 locale(compiler): Made a small contribution to internationalization

问题相关

使用下述命令在本地删除远程不存在的分支
git remote prune origin

使用下述命令删除远程文件
git rm -r —cached .setting #—cached不会把本地的.setting删除
git commit -m ‘delete .setting dir’
git push -u origin master