threads / discuss / 26105

Basic Git Questions

Subject: Basic Git Questions

## tl;dr

3 messages between Dec 19, 2010 and Dec 19, 2010.

replies: 2people: 3as markdown or json

Parag Kalra· Dec 19, 2010, 22:32 UTC · lore
Hi,
1. I generally add the files in my working directory using "git add
.". But sometimes you want to add all the files except 1 or 2 file. Is
there a shortcut way we can tell git to add all the files but exclude
a particular file.
2. Also sometime when the code is committed, I realize that I have
forgot to change or add a file. Is there a way we can we commit a file
to existing commit number with git log showing only the old git commit
number and not creating a new one for the last commit.

Cheers, Parag

Konstantin Khomoutov· Dec 19, 2010, 23:08 UTC · re: Parag Kalra · lore

Re: Basic Git Questions

On Sun, Dec 19, 2010 at 02:32:41PM -0800, Parag Kalra wrote:
> 1. I generally add the files in my working directory using "git add
> .". But sometimes you want to add all the files except 1 or 2 file. Is
> there a shortcut way we can tell git to add all the files but exclude
> a particular file.

Use `git add .` to add all the files and then `git rm --cached` those one or two you don't need to be committed. See the git-rm manual for details.

> 2. Also sometime when the code is committed, I realize that I have
> forgot to change or add a file. Is there a way we can we commit a file
> to existing commit number with git log showing only the old git commit
> number and not creating a new one for the last commit.

`git commit --amend` does exactly that -- amends the last commit (pointed by the HEAD ref). If you need to modify a commit older than the last one, you have to rebase a part of the branch starting with that commit and then edit (amend) that commit during the rebasing process. This might have certain caveats though, so refer to git-rebase manual. More on rebasing can be found in various pieces of documentation and books listed at http://git-scm.com/documentation

Thomas Rast· Dec 19, 2010, 23:15 UTC · re: Konstantin Khomoutov · lore

Re: Basic Git Questions

Konstantin Khomoutov wrote:
Show 6 quoted lines
> > 2. Also sometime when the code is committed, I realize that I have
> > forgot to change or add a file. Is there a way we can we commit a file
> > to existing commit number with git log showing only the old git commit
> > number and not creating a new one for the last commit.
> `git commit --amend` does exactly that -- amends the last commit
> (pointed by the HEAD ref).

Let me add that you can fundamentally not edit any git object in any way without its ID (hash, sha1, whatever you call it) changing. All methods of "changing" commits, like amend, rebase, filter-branch etc. always create a new object. The trick is that, especially with amend, the branch is also changed to point to the new one, so unless you have already merged or pushed that commit, this is as close to "changing" as it gets.

Tv put this more eloquently as: "you can never change; you can only rewrite and forget".

If you *have* merged or pushed the commit, you shouldn't rewrite history. It's not that hard to fix up your local merges, but if you have pushed the history and others have started working with it, it's *very* annoying for them.

-- 
Thomas Rast
trast@{inf,student}.ethz.ch

← back to recent threads