Git常用命令+Linux(持续更新)

初始化本地仓库

  • $ git init

将修改添加到暂存区

  • $ git add Changefile

修改文件模式,文件模式不同git也作为一次修改提交

  • #git config --add core.filemode false
  • 或者在 vim .git/config配置文件中修改filemode=false

选取部分修改添加到暂存区

  • $ git add --patch,用y,n选择需要的修改

将修改做一次提交

  • $ git commit -m “wrote a readme file”

和远程仓库关联

查看可抓取和推送的地址

  • $ git remote -v
origin  [email protected]:michaelliao/learngit.git (fetch)
origin  [email protected]:michaelliao/learngit.git (push)
上面显示了可以抓取和推送的origin的地址。如果没有推送权限,就看不到push的地址。

重置远程仓库地址

删除远程关联

  • #git remote rm origin

推送到远程仓库

  • $ git push origin master:master

从远程仓库拉取操作

  • $ git pull origin master:master

克隆远程仓库

查看分支

  • $ git branch

创建分支

  • $ git branch name

切换分支

  • $ git checkout name

创建+切换分支

  • $ git checkout –b name

合并某分支到当前分支

  • $ git merge branchName

查看提交日志

  • $ git reflog
  • $ git log
  • $ git log --graph --pretty=oneline --abbrev-commit(只显示简短commitId和提交信息)

删除分支

  • $ git branch –d name

回退到上一个版本

  • $ git reset --hard HEAD^

显示工作目录和暂存区状态

  • $ git status

撤销工作区的修改

  • $ git checkout .撤销工作区的一切修改,慎用!!!!
  • $ git checkout –

撤销暂存区的修改

  • $ git reset HEAD file,回到添加到暂存区后的状态

删除

删除暂存区文件
  • $ git rm --cache fileName
删除工作区的文件
  • $ rm -rf fileName
删除错误commit提交
  • $ git reset --soft HEAD~1
  • $ git reset --hard <commit_id>
  • $ git push origin HEAD --force(加force参数强制提交)
参数:
  • git reset –soft:回退到某个版本,只回退了commit的信息,不会恢复到index file一级。如果还要提交,直接commit即

  • git reset –mixed:此为默认方式,不带任何参数的git reset,即时这种方式,它回退到某个版本,只保留源码,回退commit和index信息

  • git reset –hard:彻底回退到某个版本,本地的源码也会变为上一个版本的内容

合并分支

  • $ git merge branchName

追加提交

1、将HEAD移到需要的修改的提交上面

  • $ git rebase commitID

2、修改文件

3、添加修改到暂存区

  • $ git add filename

4、追加修改到commit上

  • $ git commit --amend

5、将HEAD移动到最新的commit上

  • $ git rebase --continue

6、如果有冲突解决冲突,git add ,git commit --amend,git rebase --contiue

stash用法

  • $ git stash

  • $ git stash save ""可以加一些注释

  • 将为提交的修改保存到堆栈中

  • $ git stash list

  • 查看stash列表

  • $ git stash pop

  • $ git stash pop [–index] []

  • $ git stash pop --index stash@{0}

  • $ git stash apply 作用等于pop,不同于pop的是该命令不会将内容从堆栈中删除

  • $ git stash apply --index stash@{0}

  • 恢复保存到堆栈的提交

  • git stash drop + 名称

  • 从堆栈中移除某个指定的stash

  • git stash clear

  • 清除堆栈中的所有 内容

  • git stash show

  • 查看堆栈中最新保存的stash和当前目录的差异

猜你喜欢

转载自blog.csdn.net/xiaopeng_csdn/article/details/84775075