常用git命令记录

常用git命令记录

提交相关

git add 文件名/.是所有 # 存到暂存区
git commit -m "注释" # 把暂存区内容提交到当前分支
git push # 提交到远程分支

git stash # 通常在 git add完了还没commit(因为commit会有记录) 但是又想切换分支的适合用
git stash list # 查看git存储栈
git stash pop # 应用最近一次git stash的内容
git stash apply list中的名字 # 用对应存储栈中的内容

创建分支

git checkout -b new_branch_name # 创建新分支
git push origin remote_new_branch_name:new_branch_name # 将本地分支推送到远程分支
git branch --set-upstream-to=origin/remote_new_branch_name new_branch_name # 将本地分支和线上分支关联

合并分支代码相关

git fetch origin # 更新远程跟踪分支内容 但不影响本地
git merge xxx # 将xxx分支与当前分支合并
git push # 提交merge记录

git pull --rebase # 以上三条指令合起来 而且记得加上--rebase参数 就不会有多一条merge记录
git push origin local_branch_name:my_remote_new_branch # 将本地分支提交到远程新分支

查看git记录和回退等等(异常恢复)

git log filename # 查看提交记录 会有commit_id, 不加filename就是查看全部
git reset --hard commit_id # 回退到某次提交(--hard 本地代码会变 --soft本地代码不变)
git push origin master --force # 永远的回退到某次提交就加上这句(即master分支的那次提交之后的所有提交都撤销)

git checkout filename commit_id # 将某个文件回退到某次提交 记得push到远程
原文地址:https://www.cnblogs.com/TRY0929/p/15165570.html