学习GIT

来源:互联网 发布:c语言大于号怎么打 编辑:程序博客网 时间:2024/06/05 04:45

使用心得

  1. 阶段性修改需要暂存,又不想commit,使用 git stage/add
  2. 查看文件改动

    1. git diff                         显示当前工作区的文件和stage区文件的差异
    2. git diff --staged           显示stage区和HEAD的文件的差异
    3. git diff HEAD               显示工作区和上次递交文件的差异
  3. 使用git commit -a 之前,先使用 git status,git diff HEAD 仔细查看需要递交的内容,如果有内容想回退,使用以下方式

    1. 回退工作目录中所有未提交的修改,使用git reset --hard HEAD  
    2. 回退某一个文件的修改(没做过git stage/add暂存操作的修改),使用git checkout -- <file>

      1. 对于暂存过的修改, 则使用 git reset HEAD <file>取消暂存,然后再做git checkout -- <file>撤销修改

  4. git commit 之后,如果想撤回,请参考本文后面的《版本回退命令的区别》
  5. git push之前需要做一次git pull
  6. 使用gitk查看version tree
  7. 创建分支并进入新分支 git checkout -b newbranch
  8. 获取某一个分支(branch_x)的某一个目录下(path_x)的所有文件 git checkout <branch_x> path_x/* 
  9. git diff commit_id_1 commit_id_2 比较代码
  10. git 切分区前,需要git stash保存当前修改记录,然后切回来后敲git stash pop,将代码还原
  11. 添加一个新的远程仓库git remote add [new_repo_shortname] [url] 
  12. 将本地branch push到远程仓库 git push -u [repo_shortname] local_branch
  13. 合并commit. (参考:如何合併多個commits
    1. 运行git rebase -i HEAD~n后弹出界面
    2. 替换想要合并的commit,将pick替换成s,保存退出后,再次弹出界面
    3. 填写新的commit msg
  14. git mergetool
  15. git difftool
  16. git merge后,发现stage里有些代码不想commit,可以参考3.1.1的步骤将指定代码从stage中删除
  17. 删除及其远程分支 git branch -dr origin/bugfix && git push origin --delete bugfix (参考:Delete a Git branch both locally and remotely)
  18. # Start a new featuregit checkout -b new-feature origin/dev# Edit some filesgit add <file>git commit -m "Start a feature"# Edit some filesgit add <file>git commit -m "Finish a feature"git push origin new-feature#git push origin new-feature:new-feature# If you wish to set tracking information for this branchyou can do so with:git branch -u origin/new-feature# Merge in the new-feature branchgit checkout mastergit merge new-feature# Delete branch locallygit branch -D new-feature# Delete branch on remotegit push origin :new-feature






 

版本回退命令的区别

  • git revert 是撤销某次操作,此次操作之前的commit都会被保留

  • git reset 是撤销某次提交,但是此次之后的修改都会被退回到暂存区

    • git reset -soft : 取消了commit  

    • git reset -mixed(默认) :  取消了commit ,取消了add

    • git reset -hard : 取消了commit ,取消了add,取消源文件修改

 

0 0
原创粉丝点击