# Learn_Git **Repository Path**: note333/Learn_Git ## Basic Information - **Project Name**: Learn_Git - **Description**: 以实际情况使用Git,用最少的命令完成复杂的操作。 - **Primary Language**: Shell - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2019-10-30 - **Last Updated**: 2023-04-02 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README ### Git常用命令 - **基本命令**
点击查看 # 自报家门 git config --global user.name "Your Name" git config --global user.email "email@example.com" git init # 初始化本地仓库 # 将文件添加到暂存区中 git add # 单个文件/文件夹 git add --update # 所有修改、已删除的文件到暂存区中 git add --all # 所有文件到暂存区中(包括新增文件) # 将暂存区的文件提交到本地仓库 git commit -m "描述信息" git commit --amend # 修改上次提交的描述信息 # 保留工作现场 git stash save "save message" # 执行保留并添加备注 方便查找 git stash list # 查看保留了哪些现场 git stash show # 默认显示第一个现场做了哪些改动, 如果要显示其他现场,git stash show stash@{索引} git stash apply # 默认应用第一个现场,但不会从现场列表中删除,如果要使用其他现场 git stash apply stash@{索引} git stash pop # 默认恢复第一个工作现场并从列表中删除, 如果要恢复并删除其他现场 git stash pop stash@{索引} git stash drop stash@{索引} # 从列表中删除这个现场 git stash clear # 删除列表中所有的现场
- **查看命令**
点击查看 git status # 查看本地仓库的状态 git status -s # 以简短模式查看状态 git log # 显示所有的提交记录 git log --pretty=oneline # 单行显示所有的提交记录 git reflog # 显示所有分支的所有操作记录(包括已删除) git diff # 显示当前文件和暂存区中文件的差异 git diff # 查看从指定的版本之后改动的内容 git remote # 列出已经存在的远程仓库 git remote -v # 列出远程仓库的详细信息
- **撤销命令**
点击查看 # 还没git add # 撤销本地修改的文件: git checkout -- # 单个文件/文件夹 git checkout . # 所有文件/文件夹 # 撤销本地新增的文件: rm filename / rm dir -rf # 单个文件/文件夹 git clean -xdf # 所有文件/文件夹 # 已经git add # 撤销本地修改/新增的文件: git reset HEAD # 单个文件/文件夹 git reset HEAD . # 所有文件/文件夹 # 已经git add和git commit git reset "commit_id" # 已经提交的修改还会在工作区 git reset --hard "commit_id" # 注意: 在工作区/暂存区的代码也将会清除
- **分支命令**
点击查看 git branch # 列出本地的所有分支 git branch -v # 列出本地所有分支并显示最后一次提交 git branch -a # 查看所有分支 git branch -r # 查看远程分支 git branch # 创建分支 git branch -d # 删除分支 git branch -D # 强制删除分支 git checkout # 切换分支 git switch # 切换分支 git checkout -b # 创建+切换分支 git switch -c # 创建+切换分支 git merge # 合并指定分支到当前分支下
- **远程命令**
点击查看 git remote add origin # 关联远程仓库 git remote rename origin <新名> # 修改远程仓库的别名 git remote remove origin # 删除名为origin的远程仓库 git remote set-url origin # 修改origin远程仓库的 URL 地址 git clone # 克隆远程仓库到本地(默认只有master分支) git clone <本地目录> # 克隆远程仓库到指定目录 git clone -b dev # 克隆远程仓库的dev分支到本地 git push origin dev1:dev2 # 把本地仓库的dev1分支推送到远程仓库的dev2分支 git push origin :dev2 # 删除指定远程仓库的dev2分支 git push origin --delete dev2 # 删除指定远程仓库的dev2分支 git fetch origin # 将远程仓库所有分支的最新版本全部取回到本地 git fetch origin temp # 将远程仓库的dev分支的最新版本取回到本地的temp分支 git pull # 从远程仓库获取最新版本(git fetch + git merge)
- **切换命令**
点击查看 git log --oneline # 查看所有提交记录 git checkout 'commit hash' # 切换指定提交记录 git checkout master # 切换回当前分支
--- ### Git的使用技巧 - 克隆远程仓库的所有分支到本地: > ~~~shell script > git clone <远程仓库地址> # 默认只有master分支 > git branch -r | grep -v '\->' | while read remote; do git branch --track "${remote#origin/}" "$remote"; done > ~~~ --- ### Git多账户配置及使用 1. 新建多账户的SSH Key ``` cd ~/.ssh ssh-keygen -t rsa # 设置自定义名称 Enter file in which to save the key (~/.ssh/id_rsa): id_rsa_自定义名称 ``` 2. 添加SSH Key到SSH agent中 ``` ssh-add ~/.ssh/id_rsa_自定义名称 ``` > Tip: 如显示`Could not open a connection to your authentication agent` > > 输入`ssh-agent bash`后再次添加 3. 配置`~/.ssh/config`文件 ``` # 自定义一个Host别名 Host gitee2 HostName gitee.com User git IdentityFile ~/.ssh/id_rsa_自定义名称 ``` 4. 使用别名克隆仓库到本地(演示) ``` git clone git@gitee2:userName/repositoryName.git ```