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

11

u/Isvara Nov 05 '21

A shell is a program that lets a user run other programs, usually just by typing their name. So a shell will offer a prompt and receive input like git add. It breaks it up into pieces, looks at the first part and says, "I don't know what this is, so it must be a program on disk," then runs the program typically at /usr/bin/git, telling it, "Hey, you were called using the command line git add". Then git looks at that command line and decides what it needs to do.

So no, git is not a shell, but it is commonly launched by one. Git doesn't care what launched it: popular shells are bash, zsh and fish.

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.

6

u/Grodesby Nov 05 '21

You call git using bash, but git is a C application that does not itself run bash, but manipulates the file system directly using C.

2

u/ergotofwhy Nov 05 '21

git is a command that runs on a command line. The underlying terminal could be bash, or it could be zsh, gnome-terminal, or windows cmd.

the easiest way to think of this is that git is an executable program itself, and the commands are arguments that get passed to the program

3

u/RhoOfFeh trunk biased Nov 05 '21

git is just a command-line program. It accepts the parameters provided and does its thing.

bash, or zsh, or CMD, or whatever terminal emulator you use is simply offering you a way to run (and maybe stitch together) command-line programs, like git, grep, cat, or thousands of others.

My guess is that you downloaded "Git Bash", which is a very useful CMD replacement for windows that makes interacting with git a bit nicer thanks to default settings and utility programs. I do use it myself when on a Windows machine and find it a useful tool.

5

u/fourhundredthecat Nov 05 '21

terminal emulator

you might be confusing shell with terminal emulator

2

u/RhoOfFeh trunk biased Nov 05 '21

There's not that much difference from my POV. Just because we don't use ANSI escape sequences to address the 80x24 display doesn't mean it's not a teletype terminal.

So read 'or whatever terminal emulator you use' as an addition to the list, not a substitution for it.

7

u/arienh4 Nov 05 '21

Eh, there's a pretty massive difference. A terminal emulator is something that, well, emulates a terminal. The linux console is a terminal emulator, as is screen/tmux, and graphical ones like rxvt, xterm, Alacritty, PuTTY.

A shell is an interface that runs inside a terminal that allows you to interact with the system. That's your bash/zsh/CMD, but also Gnome, KDE, Windows Shell.

They're certainly not interchangeable.

-1

u/RhoOfFeh trunk biased Nov 05 '21

I thought I was pedantic.

I also thought I had addressed this concern with my second sentence. Yes, there's a difference. But does that matter for this discussion?

1

u/3rdRealm Nov 09 '21

Running, for example, git status, runs the same thing on bash, fish, zsh, sh, and every other shell as long as it is not aliased to something else and git is in the shell path. Git itself is a C program that gets called when git is run in the terminal.

1

u/christian-mann Nov 05 '21

I think I understand your question. Git was originally implemented as a pile of shell scripts, which used programs like ls, mkdir, cp, sha1sum, et cetera, to do the job of tracking content. Since then the core functionality has been rewritten to be actual compiled binary programs, written in C, that use the actual operating system APIs. For example, this is git-diff: https://github.com/git/git/blob/master/diff.c

However, some functionality is still implemented in terms of shell scripts, such as git-filter-branch: https://github.com/git/git/blob/master/git-filter-branch.sh

0

u/JonnyRocks Nov 06 '21

git is an application. i am guessing you are young and dont have a lot of CLI experience. but before graphic user interfaces we used command line. got is the program and add is an argument. so a commandline calculator might take. calc add 3 5 and reply with 8