Is it possible to skip the staging area and (also) commit untracked, new files to git?
Asked Answered
S

5

13

Is it possible to skip the staging area and (also) commit untracked, new files to git in a single built-in, command-line command ? If not, what are the alternatives ?

http://git-scm.com/book/en/Git-Basics-Recording-Changes-to-the-Repository

Providing the -a option to the git commit command makes Git automatically stage every file that is already tracked before doing the commit, letting you skip the git add part:

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

Thanks.

Smaragd answered 17/4, 2013 at 19:51 Comment(2)
You can specify filenames on the command line after commit as well.Crossway
@Crossway That will not commit files that are untracked. You will get the error message did not match any file(s) known to gitMessner
S
3

This might seem quite trivial for the gurus, but is a minor revelation to me (I admit) - at least I just used it for the first time now and it works (without custom aliases): Just use a semicolon ; and it'll work as a one-liner:

git add --all; git commit -m "some informative commit message"

Smaragd answered 17/4, 2013 at 21:27 Comment(0)
M
20

Using a single, built-in, command-line command? No.

Using two commands:

git add -A
git commit

Using a custom alias:

Add this to .gitconfig:

[alias]
   commituntracked = "!git add -A; git commit"

Then you can do

git commituntracked
Messner answered 17/4, 2013 at 20:0 Comment(6)
May I kindly ask how to expand that custom alias to also include the ("variable") commit message? Thx!Smaragd
Depends on what operating system you are running on. This question has some good examples #3321992Messner
Linux. Sorry, but I (at least at first glance) can't seem to string the examples on that link together and apply them here, so as to include the git commit message. Could I just add -m to give git commituntracked -m "some message" ?Smaragd
Yes, adding parameters after the alias, as in your example git commituntracked -m "some message", will work with the alias I suggested.Messner
Note that, technically, the commands I suggested don't really skip the staging area. The untracked files become tracked and do pass through the normal staging area. I interpreted your question as meaning that you wanted to get an untracked file committed with as few commands as possible, preferably one.Messner
I would prefer using && rather than ; so that if (somehow) the add fails, you won't go on to execute the commit.Crossway
S
3

This might seem quite trivial for the gurus, but is a minor revelation to me (I admit) - at least I just used it for the first time now and it works (without custom aliases): Just use a semicolon ; and it'll work as a one-liner:

git add --all; git commit -m "some informative commit message"

Smaragd answered 17/4, 2013 at 21:27 Comment(0)
H
2

Yes. There are at least two major ways of doing that. First, you don't have to use "the" staging area, you can have as many staging areas as you like -- set GIT_INDEX_FILE=/path/to/private/index and do as you please; second you can construct commits yourself, directly. It isn't even hard.

Git's repository core deals with blob, tree, and commit objects (also, not so relevant here, notes and annotated tags). The git command to dump objects is git cat-file -p.

A blob is just a bag-o-bits. Add one to the repository with git hash-object -wfilename, it'll print the ~true name~ of the blob in that file and add the blob to the repo. A tree ties an object to a filesystem name. Add one to the repository with git mktree; to see what to feed it, print a tree with e.g. git cat-file -p HEAD^{tree}. Add a commit to the repository with git commit-tree, basically, you say git commit-tree -pmom-pdad sometree, set some environment variables, and feed it a commit message on stdin.

That's really all that's necessary; if you want to get further into slicing and dicing with trees and staging read-tree and write-tree can be very useful, if this is at all attractive to you the git core tutorial is a good overview.

Hydroelectric answered 17/4, 2013 at 22:5 Comment(0)
R
0

Using the command below skips the staging area and commits directly from the working directory.

git commit -a

Note that you still need to add new untracked files.

Redistrict answered 28/4, 2020 at 12:20 Comment(0)
T
0

The following code will help to skip the staging area (Don't know if it is a good practice, if not correct me).

git commit -a -m "message"
Thwart answered 19/1 at 3:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.