I have noticed recently that, the start of my workflow involves making a git repo and adding committing, and pushing the code to the repository by typing in different commands. Although it is not an extremely cumbersome task it would be nice if there was a single command to do so. But I couldn't find any. So I was wondering is there any way to make a personalized command? If so can anybody suggest to me where can I learn more about it? I'd like to learn rather than have it spoon-fed to me.
-
3[Aliases](https://git-scm.com/book/en/v2/Git-Basics-Git-Aliases) seem to be the obvious answer. Did you rule them out for some reason? – Romain Valeri Dec 08 '21 at 08:58
-
1Another approach to do "a sequence of commands" would be a batch script (shell script, if you're moving in the Linux/Unix world). – Joachim Sauer Dec 08 '21 at 09:08
-
1One option is to create and implement an executable `git-foo` under any path included in $PATH. Then you can use `git foo`. – ElpieKay Dec 08 '21 at 09:36
-
@RomainValeri thanks, I had no knowledge of this command since I learnt about only basic git commands. This has helped me to achieve what I wanted. – Pratik Thorat Dec 08 '21 at 18:37
-
@ElpieKay I will look into it, although aliases helped me do what I want but I might look into your suggestion since I will probably customize the command to my liking. – Pratik Thorat Dec 08 '21 at 18:38
-
@JoachimSauer I haven't transitioned to Unix/Linux world yet but I am looking into it since it's considered amazing for programming related stuff. Might start with Xubuntu or fedora. Any suggestions for a novice programmer like me? – Pratik Thorat Dec 08 '21 at 18:40
1 Answers
Git requires that your system have a POSIX-compatible shell. As such, Git-for-Windows includes a port of bash, which is (or can be when run in the right modes) a POSIX-compatible shell.
This means you can write shell scripts, using the same POSIX-compatible shell that Git will use. For very short commands, consider using Git's aliases to run a shell command:
[alias]
foo = !echo foo
(or git config alias.foo "!echo foo"
from the shell / CLI, although the method of quoting text will depend on the shell or CLI in question). For longer commands, however, shell scripts, Python programs, and/or programs written in your own favorite language are probably superior. See ElpieKay's comment about naming the command and having Git locate it. Note that when your command is run, some environment variables are set (e.g., $GIT_DIR
) and $PATH
is augmented to have the git-core
directory added (git --exec-path
will show you where this is on your installation). This makes it easy for you to run Git's plumbing commands, and even some of the internal Git scripts such as git-sh-setup
.
In general, you should write scripts that use plumbing commands: these are Git commands whose behavior is predictable (does not depend on user configuration) and whose output is designed to be easily readable by other programs. Carefully distinguish between these and Git porcelain commands, which tend to read user configurations to control, e.g., whether a diff should attempt to locate renamed files, and whether output should be colorized via ANSI escape sequences (see List of ANSI color escape sequences). Output that contains somewhat unpredictable escape sequences—the colors in question are up to the user—will be difficult to interpret.
If you use bash, you can write your own shell scripts that aren't Git commands at all, as well. For instance, you could write a command that invokes curl
or gh
(the GitHub CLI) to create a repository on GitHub, which you can't do from Git itself.

- 448,244
- 59
- 642
- 775
-
-
Do you know of any repositories or blogs showcasing how to do what you explained just now ? I'm a novice guy so I don't know where to search for these things. – Pratik Thorat Dec 09 '21 at 05:34
-
1No, I'm from the era where we bought books about this stuff. Rochkind had (has?) a good Unix book for instance. – torek Dec 09 '21 at 05:51