-
Notifications
You must be signed in to change notification settings - Fork 0
Open
Labels
Description
常用命令
[TOC]
0. 初始化仓库
- Git global setup
git config --global user.name "username"
git config --global user.email "kkxujq@163.com"- Create a new repository
git clone https://gitlab.com/kkxujq/repository.git
cd leetcode
touch README.md
git add README.md
git commit -m "add README"
git push -u origin master- Existing folder
cd existing_folder
git init
git remote add origin https://gitlab.com/kkxujq/repository.git
git add .
git commit -m "Initial commit"
git push -u origin master- Existing Git repository
cd existing_repo
git remote rename origin old-origin
git remote add origin https://gitlab.com/jq/repository.git # 建立新的源名和链接
git push -u origin --all
git push -u origin --tags1. 查看配置
git config --local --list # [–local|–global|–system]
git config --list # 查看当前生效配置2. 远程仓库
# 查看远程仓库
git remote # 查看当前设置的简短远程仓库名
git remote -v # 或 `--verbose` 显示对应克隆地址
# `git remote add [shortname] [url]` 添加新的远程仓库
git remote add temp git://github.com/jq/repository.git
# `git remote rm [shortname]` 移除远程分支
git remote rm temp创建分支
git branch # 显示本地当前所有分支 前边有 * 的为当前分支
git branch -v # 显示更多 分支的信息
git branch -v -a # 显示当前使用仓库的所有分支,包括远程分支
git branch b_name # 基于当前所在的分支创建新分支
git fetch remote_name # 将仓库拉取到本地
git branch branch_name remote_name/branch # 基于远程仓库创建新分支
git checkout -b branch_name remote_name/branch # 基于远程仓库创建新分支并且切换到新分支3. 重写最后一次 commit
git add .
git commit --amend # 之后会进入编辑器,如果 commit msg 没有更改可直接 git commit --amend --no-edit4. 查看文件状态
> git status -s # --short
# 新添加的未跟踪文件前面有 ?? 标记,新添加到暂存区中的文件前面有 A 标记,修改过的文件前面有 M 标记
M README # 文件在工作区被修改了但是还没有将修改后的文件放入暂存区
MM Rakefile # 在工作区被修改并提交到暂存区后又在工作区中被修改了
A lib/git.rb # 新添加到暂存区中的文件
M lib/simplegit.rb # 被修改了并将修改后的文件放入了暂存区
?? LICENSE.txt # 新添加的未跟踪文件5. 查看已追踪存存在改变的文档
git diff --name-only6. 删除分支
git branch --delete <BranchName> # 本地分支
git push origin --delete <BranchName> # 远程分支7. 移除已提交的文件
git rm -r --cached [files]
git commit -m 'comment'8. 查看日志
git log --pretty=format:"%h|[author]%an|[user]%cn|[email]%ae|%ad|%s" --date=format:'%Y-%m-%d %H:%M:%S' --reverse [fileName]查看未提交内容
git status # 查看文件状态
git show-branch -a --color=always # 哪些提交在远程不存在
git log master ^origin/master # 查看未 push 到远程仓库的提交删除已 push 到仓库的文件
git rm -r --cached fileDir
git commit -m "delete files"
git push查看当前目录默认分支名
git symbolic-ref --short HEAD
# 或
git rev-parse --abbrev-ref HEAD用 shell 也能实现
git branch | grep \* | cut -d ' ' -f2丢弃修改
git checkout . # 本地所有修改的。没有的提交的,都返回到原来的状态
git stash # 把所有没有提交的修改暂存到stash里面。可用git stash pop回复。
git reset --hard HASH # 返回到某个节点,不保留修改。
git reset --soft HASH # 返回到某个节点。保留修改
git clean -df # 返回到某个节点
git clean 参数
-n 显示 将要 删除的 文件 和 目录
-f 删除 文件
-df 删除 文件 和 目录保存和恢复进度
-
保存进度
git stash # git stash save 'message...' 可添加说明,且多次保存进度 -
进度列表
git stash list
-
恢复进度
git stash pop [–index] [stash_id] # 恢复指定存储的进度 git stash pop stash@{1} -
删除存储进度
# 删除制定进度或最新进度 git stash drop [stash_id] # 删除所有存储的进度 git stash clear
-
查看存储内容
# 查看最近一次的存储内容 git stash show -p stash@{0}
修改分支名
-
本地分支重命名(还没有推送到远程)
git branch -m oldName newName
-
远程分支重命名(已经推送远程)
-
重命名本地分支
git branch -m oldName newName
-
删除远程分支
git push --delete origin oldName
-
上传新命名的本地分支
git push origin newName
-
删除远程分支
git branch -r -d origin/branch-name # 删除了本地的远程跟踪分支
git push origin :branch-name # 删除项目内包含子模块
项目子模块也拥有一个独立的 git 记录,这种情况下可采用 git submodule 来管理子模块的开发。
- 添加子模块
git submodule add https://github.com/demo.git
- 初始化仓库
git submodule foreach git pull
更多内容参看官方文档:link:
解决方案
1. 提交本地创建但远端仓库不存在或未关联的分支代码
> git push
fatal: The current branch master has no upstream branch.
To push the current branch and set the remote as upstream, use
git push --set-upstream origin master
解决方法一:远程不存在当前分支
git push -u origin master以上命令会在远程自动创建分支并关联。
解决方法二:远程存在分支目标分支时 git push 时指定分支即可
git push --set-upstream origin masterReactions are currently unavailable