git的操作记录

一.让.gitignore文件生效

1.git要排除一些文件或者文件夹,比如我的项目用phpstrom开发,会有一个.idea目录,需要忽略。如果.idea目录还没有加入到git,则可以直接在.gitignore文件添加,但是如果.idea目录已经在版本管理了,修改.gitignore就不会生效了,需要执行下面命令

git update-index --assume-unchanged   .idea/*.xml

对于“.gitignore”文件,官方是这样介绍的:

gitignore - A gitignore file specifies intentionally untracked files that Git should ignore. Files already tracked by Git are not affected.(gitignore文件指定了Git应该忽略的有意未跟踪的文件。已经通过Git跟踪的文件不受影响)

还有一种方式,让修改后的.gitignore文件生效

git rm -r --cached .
git add .
git commit -m "update .gitignore"

二.git pull冲突解决

2.git stash save "保存的本地修改" 保存本地的修改到stash

有时候本地做了修改,远程仓库也做了修改,执行git pull的时候就会报错

$ git pull
error: Your local changes to the following files would be overwritten by merge:
        .gitignore
        .idea/workspace.xml
Please commit your changes or stash them before you merge.
Aborting
Updating 5af7fb0..222f517

这时候就要把本地的修改保存到stash中。

先执行git stash save "我的修改".

再执行git pull

3.合并本地和远程的修改,git stash pop会把最后一个stash的修改和刚才pull拉下来的版本进行合并,如果有冲突会显示出来

$ git stash pop
Auto-merging .idea/workspace.xml
CONFLICT (content): Merge conflict in .idea/workspace.xml
Auto-merging .gitignore
The stash entry is kept in case you need it again.

冲突显示.idea/workspace.xml冲突,git会把两个版本的冲突信息都写到这个文件中,你需要编辑冲突文件

4.手动编辑文件,解决冲突

<<<<<<< HEAD 
Creating a new branch is quick & simple.
======= 
Creating a new branch is quick AND simple.
>>>>>>> feature1

比如这是一个冲突 <<<<<< HEAD   到=====部分是远程版本的修改, =====到>>>>>是本地版本的修改,你需要编辑需要留下的代码,并且把这些标识删除。

5.编辑好冲突文件,然后git add 

git add .idea/workspace.xml
git commit -m "解决workspace.xml冲突"
git push

6.这就解决了

三,撤销修改  git add之前撤销修改(就是撤销工作区的修改)

1.先看看有哪些文件修改过 git status,红色的就是git add之前修改的文件

$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
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:   public/js/app.js
        modified:   resources/js/components/global/Navigation.vue

2.git checkout 文件名,就会撤销修改,回到上一次git add时的状态

$ git checkout public/js/app.js
$ git checkout resources/js/components/global/Navigation.vue

3.这是你git status发现,这两个红色的文件没有显示了 

四,撤销修改  git add之后 (就是撤销暂存区的内容)

1.先看修改的文件 git status,绿色的就是git add后的文件

$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        new file:   resources/js/api/user.js
        modified:   resources/js/components/global/Navigation.vue

2. git reset HEAD  把暂存区的修改返回给工作区,git status文件显示红色了

猜你喜欢

转载自blog.csdn.net/littlexiaoshuishui/article/details/104994966