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.
CodePudding user response:
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.