git 基楚操作

本文深入解析Git的基本操作流程,包括状态检查、文件追踪、修改提交、推送更新等关键步骤。详细介绍了如何使用git status、git add、git commit、git push等命令,并解释了暂存区的概念及如何处理文件修改与新增。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

(自己操作自己的项目,本地用 git commit -a -m "msg" + git push 服务器用 git pull. 很简便的操作)

git status

1.clean

On branch master

nothing to commit, working directory clean

 

2.added file,file untracked

未跟踪的文件意味着Git在之前的快照(提交)中没有这些文件;Git 不会自动将之纳入跟踪范围,除非你明明白白地告诉它“我需要跟踪该文件”,因而不用担心把临时文件什么的也归入版本管理。

On branch master

Untracked files:

    (use "git add <file>..." to include in what will be committed)

           README

nothing added to commit but untracked files present

    (use "git add" to track)

3.new file added

只要在 “Changes to be committed” 这行下面的,就说明是已暂存状态(staged)。

On branch master

Changes to be committed:

     (use "git reset HEAD <file>..." to unstage)

            new file: README

 

4.tracked file modified

On branch 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: benchmarks.rb

 

5.modify staged file

如果现在提交,那么提交的是staged的版本,而非当前工作目录中的版本。所以,运行了 git add 之后又作了修订的文件,需要重新运行 git add把最新版本重新staged起来

On branch master

Changes to be committed:

     (use "git reset HEAD <file>..." to unstage)

          new file: README

          modified: benchmarks.rb

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: benchmarks.rb

 

 

git add [files]

1.add [modified files] to staged area

2.track [new files]

 

可以用它开始跟踪新文件,或者把已跟踪的文件放到暂存区,还能用于合并时把有冲突的文件标记为已解决状态等

 

git commit

1.这种方式会启动文本编辑器以便输入本次提交的说明

$ git commit

默认的提交消息包含最后一次运行 git status 的输出

# Please enter the commit message for your changes. Lines starting # with '#' will be ignored, and an empty message aborts the commit.

# On branch master

# Changes to be committed:

#       new file: README

#       modified: benchmarks.rb

#

~

~

~

".git/COMMIT_EDITMSG" 10L, 283C

 

2.

$ git commit -m "Story 182: Fix benchmarks for speed"

 

3.跳过使用暂存区域的方式,直接提交 -a

$ git commit -a -m 'added new benchmarks'

 

 

git push [remote-name] [branch-name]

$ git push origin master

 

 

git update-index --assume-unchanged [filename]

.gitinore 不能忽略库上的文件,用这个可以忽略本地修改了的文件,不再显示modified,不需要提交更新,不影响其他人。

*但是之后用git stash 也会把这个忽略 stash 掉。

 

git diff

git diff 比较的是工作目录中当前文件和暂存区域快照之间的差异(not staged vs staged)

git diff --cached 已经暂存起来的文件和上次提交时的快照之间的差异 (高版本的git diff --staged 一样)

 

 

git rm

untrack file

1.从暂存区移除

$ git rm grit.gemspec

 

.gitignore

.gitignore只能忽略那些原来没有被track的文件,如果某些文件已经被纳入了版本管理中,则修改.gitignore是无效的。

$ cat .gitignore

*.[oa](忽略所有以 .o 或 .a 结尾的文件)

*~(忽略所有以波浪符(~)结尾的文件)

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值