GIT
git stash
// 重新应用缓存的stash,默认{0}
git stash pop
// 给stash加一个message,用于记录版本(只保存modified)
git stash save "xxx-stash"
// 给stash加一个message,用于记录版本(保存modified和new create)
git stash save -u "xxx-stash"
// 查看现有stash
git stash list
// 重新应用指定序号的stash缓存
git stash pop stash@{1}
git branch
// 删除远程分支
git push origin --delete <remote_branch_name>
// 强制更新代码到指定远程分支
git push origin <remote_branch_name> -f
// 同步本地远程分支
git remote update origin --prune
// 分支重命名
git branch -m old_branch new_branch # 重新命名本地分支
git push origin :old_branch # 删除远程旧分支
git push --set-upstream origin new_branch # 将本地分支退向远程仓库, 将本地分支track到新的远程分支
git reset
// 回滚本地最近次2的提交
git reset --soft HEAD~2
// 回退到指定的commit_id,可以使用git log查看
git reset --hard <目标commit_id>
// 回退到上一个版本
git reset --hard HEAD
git commit
// 修改最新的commit,其他场景请查看文末TIPS
git commit --amend
git config
// 设置优先级
git config --local -l // 查看仓库配置,优先级最高
git config --global -l // 查看用户配置,优先级次之
git config --system -l // 查看系统配置,优先级最低
// 忽略文件权限的配置
git config core.filemode false
git tag
git tag v1.0
git tag // 列出当前所有tag
git tag -l "v1.0*" // 使用-l or --list进行模糊匹配
git push origin <tagname> // 将制定tag推送到远程分支
TIPS
gitignore文件没有生效
如果你要忽略的文件a.py已经添加到待提交到暂存区。这时如果在.gitignore中添加上a.py,对它的忽略是不生效的。这时可以使用git rm -r --cache
清空本地命令缓存,status查看下再add。
合并两个commit
比如你提交代码后,发现漏提交了文件,不想发生两个commit,那么就可以使用git commit --amend
命令把新的内容添加到之前的commit里面。
--> git add forget.py (把漏提交的文件加到暂存区)
--> 执行git commit --amend -m "这里填写提交的注释"
branch和tag
首先,branch和tag的都是基于commit id的。
tag是用于记录那个commit的代码,相当于一个快照,缩减了commit,且不可以修改。
如果一定要修改,需要基于它拉一个新branch,checkout出来的branch是可以灵活修改的。