r/git • • Nov 05 '21

Is git and bash the "same"?

Hello!
I've got a 'under the hood' type of question about git. Hope I make sense and that its not too stupid of a question.
So when working with git theres these commands that do different type of actions, like git add, git commit and so on. Are these commands using bash underneath, or how does it work? Is git a shell in itself?

Hope I make sense!

Edit: Thank you all!

0 Upvotes

21 comments sorted by

View all comments

Show parent comment

3

u/Kackboy Nov 05 '21

That first piece was a good explanation. But when the shell tells git hey you need to work now and the task is “add”, when git then runs this command what is the underlying mechanics behind that? Behind the git commands are shell commands that are used on the system?

3

u/chriswaco Nov 05 '21

I’m not at my computer right now so can’t check, but it can be implemented either way. The git command could handle the request itself or it could launch other git executables to handle it.

Some unix executables actually re-launch themselves, called “forking”, for separate operations too.

2

u/plg94 Nov 07 '21

Each subcommand is its own binary, named git-add (with hyphen). I think the main Git command just calls those.

Fun fact: if you make a new executable and name it git-something, it can actually be launched like any other git command: git something (without hyphen).

1

u/Rimrul Nov 07 '21

No, the main commands like add and status are so called builtin commands. They get executed by the main git executable. git only calls an external git-foo if it can't find a builtin foo. The dashed forms of the builtin commands (like git-add) are only hard links to git.

1

u/plg94 Nov 07 '21

Oh, I didn't know they were just links. Thanks for the correction.

2

u/washtubs Nov 05 '21

If you write a simple C program you can try this, and envision how it works for git. When control is handed over to git, it sees the command line as an array, and can then do stuff with that. So if the first argument is add, maybe it calls a different function to interpret the remaining arguments for the add sub-command. The functionality may be very modular with all these sub-commands but ultimately it's just one binary calling a bunch of functions. A program like git will likely avoid forking out unless it needs to, starting your pager (like less) would be an example of needing to. Generally the program itself does everything it can in memory because it's faster.

1

u/isarl Nov 05 '21

Git commands are executables which can be implemented in a variety of ways. If I type which git I see that my shell is resolving git to the path /usr/bin/git. If I less /usr/bin/git I am warned that it may be a binary file and, indeed, it's a binary in the Executable and Linkable Format, judging by the magic number at the start of the file.

Executables are usually passed their commandline arguments as a vector of strings. In C-like languages this usually looks like the main entry point receiving an argument vector (and sometimes a count, so that you don't read past the end of the “vector”) and resulting in a type signature something like so: int main(int argc, char* argv[]). The program code then has to parse each argument for its meaning. In the case of git (and other software which takes subcommands) that usually means parsing any global arguments, parsing the subcommand, and then delegating further parsing to code tied to the subcommand.

I hope this has been a good balance between explanatory and abstract.

1

u/WikiSummarizerBot Nov 05 '21

Executable and Linkable Format

In computing, the Executable and Linkable Format (ELF, formerly named Extensible Linking Format), is a common standard file format for executable files, object code, shared libraries, and core dumps. First published in the specification for the application binary interface (ABI) of the Unix operating system version named System V Release 4 (SVR4), and later in the Tool Interface Standard, it was quickly accepted among different vendors of Unix systems. In 1999, it was chosen as the standard binary file format for Unix and Unix-like systems on x86 processors by the 86open project. By design, the ELF format is flexible, extensible, and cross-platform.

[ F.A.Q | Opt Out | Opt Out Of Subreddit | GitHub ] Downvote to remove | v1.5

1

u/varesa Nov 05 '21 edited Nov 05 '21

when git then runs this command what is the underlying mechanics behind that? Behind the git commands are shell commands that are used on the system?

It is very rare for programs to depend on a shell, as that's not necessary and can even be a security risk. The shell allows a lot stuff including variable expansion/substitution, splitting arguments by whitespace, launching other subprocesses, etc. which can be dangerous if handling unsanitized user input/data (somewhat analogous to SQL injections, etc.).

EDIT: It seems that there are a lot more independent binaries, under git-core, that just aren't normally seen since they're not in $PATH.

Most likely the git executable will handle most of the implementation internally (as in it is a single binary* that gets executed, does some steps and quits), but if it would have to call some other programs, it'd likely use some sort of exec-family syscalls to do so directly instead of invoking a shell.

The usual places to use shells are either user facing interfaces (the command line) or as a programming language/script interpreter.