天天看点

git-提交代码-删除部分提交错误的文件夹-版本回退1.常规提交2.删除文件3.版本回退(本地及远程分支回退)4.git丢弃本地修改的所有文件(新增、删除、修改)

git常用的几个提交代码相关的命令:

1.常规提交

1.修改的文件全部提交

$ git add .

$ git commit -m "xxxx"

$ git push 

       如果是新分支,会有提示:

       然后你按照提示进行操作:git push --set-upstream origin poiReview

其中poiReview 是分支的名字。

2.添加一个或者多个文件:

代码开发完后,使用$ git  status 查看修改了哪些文件,然后选择想要提交上的文件进行git add,使用空格隔开不同文件。

(tf) localhost:xProject wang$ git status
On branch wang
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   test1.py
        modified:   test2.py

no changes added to commit (use "git add" and/or "git commit -a")
(tf) localhost:xProject wang$ git add test1.py test2.py
(tf) localhost:xProject wang$ git commit -m 'add files'
(tf) localhost:xProject wang$ git push 
           

2.删除文件

如果想要提交的文件中包含了一些你不想提交的文件夹:

$ git rm -r filename

然后再重新提交,此时PR中便不包含那些文件了。其中rm是删除的意思,-r表示循环删除文件的参数。

3.版本回退(本地及远程分支回退)

本地修改:

$ Git reset --hard commit_id 

修改HEAD、index、workspace。

加不加--hard的区别: 加上,不会出现在分支上修改的代码,可称为“干净的分支”;不加,会出现有“绿色,未提交的代码”,也就是回退版本上有修改。

此时有个疑问:远程的代码版本超前本地的代码版本,如何对远程的代码进行版本回退?

$git push origin HEAD --force

可以强制推到分支,但是要提前确认这个分支没有别人用。如果没有强制提交,只是普通提交的时候,会提示说:然你先merge代码,然后再push,所以直接强制push。

4.git丢弃本地修改的所有文件(新增、删除、修改)

4.1 使用 

git checkout

 撤销本地修改

即放弃对本地已修改但尚未提交的文件的修改,还原其到未修改前的状态。

注意: 已 

add

commit

 的文件不适用个方法,应该用本文提到的第二种方法。

git checkout .      # 撤销对所有已修改但未提交的文件的修改,但不包括新增的文件
git checkout [filename]     # 撤销对指定文件的修改,[filename]为文件名
           

4.2使用 

git reset

 回退项目版本

可以回退到任意已经提交过的版本。已 

add

 / 

commit

 但未 

push

 的文件也适用。

git reset --hard [commit-hashcode] # [commit-hashcode]是某个 commit 的哈希值,可以用 git log 查看

           

参考:

1.https://blog.csdn.net/Dandelion_drq/article/details/51259831