HA

git 常用命令

Tags:

常用命令

# 拉取代码
git pull --all
# 提交dev分支到远程仓库
git push origin dev
# 将某一个提交的变更应用到代码库
git cherry-pick -n bd61ad98
# 将某区间提交的变更应用到代码库
git cherry-pick -n bd61ad98^..ade56rq1
# 强制回退到上一个commit(未push)
git reset --hard HEAD^
# 强制回退到上上个commit(未push)
git reset --hard HEAD~2
# 回退到上一个commit, 但保留变更后的文件
git reset HEAD^
# 取消某个版本的修改
git revert <commit hash>

使用ssh连接git

# 生成ssh key
ssh-keygen -t ed25519 -C "[email protected]"
# 把ssh公钥(pub后缀的文件)加入到git账户
# 把ssh key加入到ssh-agent
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519
# 测试
ssh -T [email protected]

# 杀掉所有ssh-agent进程
# 因为每开一个terminal就要跑这些命令,开始一个ssh-agent,运行一段时间之后,后台就会有很多进程
# bash
ps -ef | grep ssh-agent | grep -v grep | awk '{print $2}' | xargs kill -9
# windows
taskkill /F /IM ssh-agent.exe /T

git 將文件轉移位置

git mv 2017032003.md csharp

查看某一次提交的文件列表

git show --pretty="" --name-only bd61ad98

查看某一个文件的提交历史

git log --follow -- filename

同时在多个分支工作

git worktree add -b hotfix ../hotfix
git worktree list
pushd ../hotfix
# working on hotfix branch
popd
git worktree remove ../hotfix
git worktree prune

查看stash的文件具体内容

git stash show -p

stash单个文件

git stash -- filename.ext

恢复在以前某个版本被删除的文件

### 先找到被删除时的commit
git log -- <filename>
git checkout <deletion commit hash>~1 -- <filename>

TortoiseGit也是很好用的

生成patch,应用patch,以及查看diff,blame,gui还是很好用的

删除 git 远程仓库中忘记加入 .gitignore 的资源

git rm --cached remove_file
# add file to gitignore`
git add .gitignore`
git commit -m "Excluding"`

修改分支和远程分支名

git branch -m <old_name> <new_name>
git push <remote> --delete <old_name>
# OR shorter
# git push <remote> :<old_name>
git branch --unset-upstream <new_name>
git push <remote> <new_name>
git push <remote> -u <new_name>

clone时指定用户名

git clone https://[email protected]/username/repository.git

# 使用token进行clone(其实跟上面没有本质区别,token本来就是用来代替密码的)
git clone https://username:[email protected]/username/repository.git

git bash中使用sh命令时出现警告”__git_ps1: command not found”

找到completion/git-prompt.sh,在~/.bash_profile或者内部引用的配置文件中添加source /path/git-prompt.sh

导出svn patch

git diff --no-prefix > ~/some-feature.diff

git rebase

# 把多个commit合并成一个
git rebase -i <待合并commit的前一个commit>
# 如果已经push到了remote,rebase完成后需要强制push
git push -f

reference

  1. 20. Moving files
  2. How do I list all the files in a commit?
  3. How to list all commits that changed a specific file?
  4. Git - git-worktree Documentation
  5. How to Use Git Worktree | Checkout Multiple Git Branches at Once
  6. See what’s in a stash without applying it
  7. How to cherry-pick multiple commits
  8. Stash just a single file
  9. Git: How to remove file from index without deleting files from any repository
  10. How do I provide a username and password when running “git clone [email protected]”?
  11. -bash: __git_ps1: command not found
  12. svn patches from git
  13. Restoring deleted files in Git
  14. How to kill all processes with the same name in Windows - Quora
  15. version control - How do I undo the most recent local commits in Git? - Stack Overflow
  16. repository - How do I rename both a Git local and remote branch name? - Stack Overflow
  17. これで完璧! 図解でわかるgit rebaseの2つの使い方!