开发者

Using Git, if I have 20 changes and I add - commit - push 5 of them. Will the other 15 still be there?

开发者 https://www.devze.com 2023-04-02 05:22 出处:网络
I want to make sure my changes are safe when I add \"some\" of my changes and commit - push them to the branch开发者_运维知识库. I am working on separate tasks and don\'t want to add - commit - push y

I want to make sure my changes are safe when I add "some" of my changes and commit - push them to the branch开发者_运维知识库. I am working on separate tasks and don't want to add - commit - push yet for some.

Thanks!


Yes, git is an excellent system for this workflow because of the "staging area" or "index". So long as you don't add -a to git commit, only the files that you have added with git add will have their version updated in the next commit. (When you git commit, the tree that's recorded in the commit is the state of the index.)

In fact, you can do finer than per-file granularity - I frequently only stage some changes from a particular file by using:

git add -p whatever.c

... and just selecting particular changes. (If the changes it offers you aren't fine enough, you can press s to split the change down. If that's still too much, you can use e to edit precisely the change that will be staged.)

As you're staging things, it's helpful to frequently do:

  • git diff

and:

  • git diff --cached

Broadly speaking, git diff shows you the changes that haven't been staged yet, while git diff --cached shows you the changes that have been staged.


Adding, committing and pushing don't alter your working tree at all; they're perfectly safe.

If you're working on seperate tasks and don't want un-staged/un-committed changes getting mixed together (or you want to keep un-committed changes for several branches), you can also use git stash to keep your working tree changes organised.

0

精彩评论

暂无评论...
验证码 换一张
取 消