Git常用命令

111 阅读1分钟

配置

中国下载地址 : git-scm.com/

官方下载地址 : gitforwindows.org/

本地使用ssh连接git

#全局设置
git config --global user.name=myusername
git config --global email=example.com

#查看现有的密钥
$ ls -al ~/.ssh
# Lists the files in your .ssh directory, if they exist

#生成密钥,邮箱为可选
ssh-keygen -t ed25519 -C "your_email@example.com"

#复制公钥到git
$ clip < ~/.ssh/id_ed25519.pub
# Copies the contents of the id_ed25519.pub file to your clipboard

#测试ssh连接
$ ssh -T git@github.com
# Attempts to ssh to GitHub

#连接出错,查看问题
$ ssh -Tvvv git@github.com
# debug ssh


提交后端代码

git status
git add .
git status
git commit -m "创建后端"
git push

使用

1. git 教程

代码托管平台:git.acwing.com

1.1. git 基本概念

  • 工作区:仓库的目录。工作区是独立于各个分支的。

  • 暂存区:数据暂时存放的区域,类似于工作区写入版本库前的缓存区。暂存区是独立于各个分支的。

  • 版本库:存放所有已经提交到本地仓库的代码版本

  • 版本结构:树结构,树中每个节点代表一个代码版本。

所谓工作区和分支独立:

  • ❌每一个工作区、暂存区都有存有一个单独的分支结构
  • ❌每一个分支节点都有独立的工作区、暂存区
  • ✅所有节点使用相同的工作区和暂存区

1.2 git 常用命令

全局设置

  1. git config --global user.name xxx:设置全局用户名,信息记录在~/.gitconfig文件中

  2. git config --global user.email xxx@xxx.com:设置全局邮箱地址,信息记录在~/.gitconfig文件中

  3. git init:将当前目录配置成 git 仓库,信息记录在隐藏的. git 文件夹中 本地命令

  4. git add XX:将 XX 文件添加到暂存区

    • git add .:将所有待加入暂存区的文件加入暂存区
  5. git rm --cached XX:将文件从仓库索引目录中删掉 解释

  6. git commit -m "给自己看的备注信息":将暂存区的内容提交到当前分支

  7. git status:查看仓库状态

  8. git diff XX:查看 XX 文件相对于暂存区修改了哪些内容

  9. git log:查看当前分支的所有版本 解释

    • git log --pretty=oneline:用一行来显示
  10. git reflog:查看 HEAD 指针的移动历史(包括被回滚的版本)

  11. git reset --hard HEAD^git reset --hard HEAD~:将代码库回滚到上一个版本 解释

-   `git reset --hard HEAD^^`:往上回滚两次,以此类推
-   `git reset --hard HEAD~100`:往上回滚 100 个版本
    
-   `git reset --hard 版本号`:回滚到某一特定版本
    

12. git checkout — XXgit restore XX:将 XX 文件尚未加入暂存区的修改全部撤销 解释 云端命令

  1. git remote add origin git@git.acwing.com:xxx/XXX.git:将本地仓库关联到远程仓库 解释

  2. git push -u (第一次需要-u以后不需要):将当前分支推送到远程仓库 解释

-   `git push origin branch_name`:将本地的某个分支推送到远程仓库
  1. git clone git@git.acwing.com:xxx/XXX.git:将远程仓库 XXX 下载到当前目录下 解释

  2. git checkout -b branch_name:创建并切换到branch_name这个分支

  3. git branch:查看所有分支和当前所处分支,带*的表示当前所在分支 - git branch -a可以查看隐藏分支

  4. git checkout branch_name:切换到branch_name这个分支

  5. git merge branch_name:将分支branch_name合并到当前分支上

  6. git branch -d branch_name:删除本地仓库的branch_name分支

  7. git branch branch_name:创建新分支

  8. git push --set-upstream origin branch_name:设置本地的branch_name分支对应远程仓库的branch_name分支

  9. git push -d origin branch_name:删除远程仓库的branch_name分支

  10. git pull:将远程仓库的当前分支与本地仓库的当前分支合并

-   `git pull origin branch_name`:将远程仓库的`branch_name`分支与本地仓库的当前分支合并
  1. git branch --set-upstream-to=origin/branch_name1 branch_name2:将远程的branch_name1分支与本地的branch_name2分支对应

  2. git checkout -t origin/branch_name 将远程的branch_name分支拉取到本地 stash命令

  3. git stash:将工作区和暂存区中尚未提交的修改存入栈中

  4. git stash apply:将栈顶存储的修改恢复到当前分支,但不删除栈顶元素

  5. git stash drop:删除栈顶存储的修改

  6. git stash pop:将栈顶存储的修改恢复到当前分支,同时删除栈顶元素

  7. git stash list:查看栈中所有元素

场景

clone

git clone url 后发现只有main分支:

  • 使用git branch -a 查看所有分支,假设显示一下内容
* refactor-with-go
  remotes/origin/HEAD -> origin/refactor-with-go
  remotes/origin/dev-change-ui2antd
  remotes/origin/develop
  remotes/origin/login
  remotes/origin/master
  • 使用git checkout -b develop origin/develop 将远程分支与本地分支联系起来
    • 或者使用git checkout -t origin/develop 本地会自动建立一个和远程分支名字一样的分支
  • git branch会出先develop分支了
* develop
  refactor-with-go

翻译

一些描述翻译~

  • git rm --cached XX 后 使用git status Your branch is up to date with 'origin/main'. 你的分支是最新的'origin/main'。

nothing added to commit but untracked files present 没有添加到提交中,但存在未跟踪的文件

  • git commit -m "" 后使用git status Your branch is ahead of 'origin/main' by 1 commit. 你的分支比'origin/main'领先1次提交。

nothing to commit, working tree clean 没有什么可持久化的,工作树干净

gitignore

设置全局.gitignore文件 常用的gitignore文件

点击T,查找特定名称,如Windows,找到 Windows.gitignore文件

然后cd ~ 到主目录,vim .gitignore_global,创建全局ignore文件,将刚刚找到的内容复制进去。

References