31

I have a git repo. And after each major change, that I make in the codebase, what I do is that I go to the terminal and execute a set of commands.

git add .
git commit -m 'some message'
git push origin master

These are the same each day and the process is quite boring. Can anyone suggest a way to somehow automate this process?

I am running a Linux Mint 14 OS.

IcyFlame
  • 5,059
  • 21
  • 50
  • 74
  • 8
    While it's good that you are using Git as version control, if all you are doing is checking in the state of your code at the end of the day as one big chunk, you're missing out on a lot of the benefits of using a version control system. – Abizern May 23 '13 at 08:58
  • 3
    Yes, you really should frequently commit logical units. See for example [How often to commit changes to source control?](http://stackoverflow.com/questions/107264/how-often-to-commit-changes-to-source-control) – sleske May 23 '13 at 09:04
  • 5
    And BTW, I think it's unfair to downvote the question. It's fine as a question, it's just based on a false premise. – sleske May 23 '13 at 09:05
  • 1
    @Abizern: what i had in mind was just a rough example. Obviously, I know the merits of VCS. But thanks anyway for pointing it out! – IcyFlame May 23 '13 at 09:20
  • @sleske The premise of the question is false, it was asked just to provide an answer which, tbh, isn't that good. The real question is "how to automate git tasks" of which there are many answers alread related to aliases, git-aliases, small scripts etc. – Abizern May 23 '13 at 10:05
  • possible duplicate ;; http://stackoverflow.com/questions/1675464/how-can-i-combine-these-git-commands – dreftymac Oct 25 '14 at 04:00
  • Does this answer your question? [How can I combine these git commands?](https://stackoverflow.com/questions/1675464/how-can-i-combine-these-git-commands) – miken32 Jan 12 '20 at 22:42

9 Answers9

37

You can very easily automate this using Bash scripting.

git add .

echo 'Enter the commit message:'
read commitMessage

git commit -m "$commitMessage"

echo 'Enter the name of the branch:'
read branch

git push origin $branch

read

store the above code as a .sh file(say gitpush.sh)

And since you have to make this sh file an executable you need to run the following command in the terminal once:

chmod +x gitpush.sh

And now run this .sh file.

Each time you run it, It will ask you for the commit message and the name of the branch. In case the branch does not exist or the push destination is not defined then git will generate an error. TO read this error, I have added the last read statement. If there are no errors, then git generates the pushed successfully type-of message.

IcyFlame
  • 5,059
  • 21
  • 50
  • 74
  • 2
    Upvoted. Could be improved with `dmenu` or `slmenu` though : `branch=$(git branch | cut -c3- | dmenu -p "Select the branch to push: ")`, since the branch name is effectively an 'enumeration' rather than an arbitrary string. – kampu May 23 '13 at 08:58
  • 1
    Yes, and that stuff that into `~/.bash_logout` for complete autopilot. Or take Abizern's comment above to heart. – Benjamin Bannier May 23 '13 at 09:00
  • If you're just going to ask question just to answer it yourself, it's probably better as a blog post. – Abizern May 23 '13 at 09:00
  • 7
    This is a rather poor implementation. First, it lacks error handling, so the script keeps trucking along even though a command failed. Secondly, it's prompting the user more than it needs to. `git commit` without the `-m` will bring up an editor, which is a much more useful environment to be in when writing a useful log message. Also, you prompt the user for the branch name, and that's really unnecessary too. You could use the output of `git symbolic-ref --short HEAD`, or better yet, make it a tracking branch so that `git push` with no other options works correctly. – John Szakmeister May 23 '13 at 09:18
  • 3
    @Abizern It's quite a usual thing to do, to ask and answer your own questions. I think there's even a badge for it. People are more likely to visit here than his blog. If he has one. – Tom Tanner May 23 '13 at 10:56
  • 1
    People ask a question, look at some of the answers and then come up with their own solution and post it. That is normal. But asking a hypothetical question (a bad one) and then answering it (badly) just clutters the site. – Abizern May 23 '13 at 11:17
  • @Abizern: http://meta.stackexchange.com/questions/17463/can-i-answer-my-own-questions-even-those-where-i-knew-the-answer-before-asking – BoltClock May 24 '13 at 08:30
  • @BoltClock Yep. I think I'm a bit harsh on this one, but I now agree that it's within the spirit of SO. However, I still think it's a poor question and a poor answer, so I've also gone within the spirit of SO and downvoted. – Abizern May 24 '13 at 08:37
12

You could use alias and shell function together, in $HOME/.gitconfig or .git/config

[alias]
        newalias = "!f(){ git add . && git commit -m \"$1\"; git push origin master; };f"

and call it as

$ git newalias "message ..."
Alper
  • 12,860
  • 2
  • 31
  • 41
12

Commiting your work once a day as a big chunk is not an effective use of version control. It's far better to make small, regular, atomic commits that reflect steps in the development of the code base (or whatever else is being tracked in the repository)

Using a shell script is perhaps overkill - most people use aliases to automate git commands, either as shell aliases or git aliases.

In my case I use shell aliases - a couple of which are:

alias gp='git push'
alias gc='git commit'
alias gca='git commit -a'
alias gcm='git commit -m'
alias gcam='git commit -am'

so for me to do what you're asking would be:

gcam "My commit message";gp

This isn't a lot of typing and is hardly boring, I do this and similar git operations literally dozens of times a day (I know - because I check my shell history).

The reason for using small aliases rather than a script is so that I have the flexibility to use different ways of committing, If I used a script I would lose that flexibility.

Abizern
  • 146,289
  • 39
  • 203
  • 257
  • 1
    Great answer in that you took the time to explain the benefits of your workflow, typical conventions and how it relates to the use of aliases. However, "-a" does not add new files like "." does. Also, it would be good to explain how an alias is created. – P.Brian.Mackey Feb 01 '15 at 13:15
  • the `gp` alias does not specify `origin master` but it works as the question asks? – mLstudent33 Sep 24 '21 at 06:55
  • 2
    @mLstudent33 It doesn't need to. The shortcut is so that I don't have to type `git push`, I can add other options after it, for example `gp origin master` or `gp origin main` or `gp origin ` – Abizern Sep 25 '21 at 08:36
11

That is not a lot of typing to do but the process can be simplified by the usage of aliases.

edit $HOME/.gitconfig to add some shortcuts, e.g. to use git p as an alias for git push origin master, use:

[alias]
        p = push origin master

If you're on a descent shell like bash, you can use the history feature. Everything you need to know is in the man page for bash.

Read more about aliases here

On a side-note, git add . is probably not the best approach since it adds every file in the folder to the staging area. Better to use git add -u to only add those files already in the index.

Fredrik Pihl
  • 44,604
  • 7
  • 83
  • 130
10

The best solution for windows: 1) create a text file anywhere on you system and insert the following code into it after changing the fields according to your need

git add .

git commit -m "commitMessage"

git push url master

exit

2) save and exit file.

3) change the file name to push.sh.

4) navigate to file directory on your system using git powershell then use the following command chmod +x push.sh to make your file runnable.

5) click windows start and search for Task Scheduler and press create a new task.

6) click on the action and browse to your push.sh file press okay.

7) navigate to triggers and modify scheduling time.

Now you have a script which is automated as you wish.

E.Zolduoarrati
  • 1,539
  • 2
  • 9
  • 9
2

For my workflow, git "adds" and git "commits" are targeted and atomic, so rolling those into a single bulk command isn't useful. However, each day, I push to a number of different branches across a number of different repositories, and that usually follows the pattern of "git push origin {branchname}". That can be automated in a shell script:

parse_git_branch() {
  git symbolic-ref HEAD 2>/dev/null | cut -d"/" -f 3
}
git push origin $(parse_git_branch)

You could then add a bash alias (e.g., "git-push") to call this script, which would effectively run "git push origin {currently checked out branch}" on whichever repository you're located in.

markfullmer
  • 201
  • 1
  • 4
1

My bash script will pick your current branch you're on and also pull to avoid conflicts from the same branch and then push. This script gets really helpful in case you're going to work between/across many branches.

git add .
read -p "Enter commit message: " commit_message
git commit -m "$commit_message"
BRANCH=$(git describe --contains --all HEAD)
git pull --rebase origin "$BRANCH"
git push origin "$BRANCH"

Way to use:

  • Put this script in a bash or shell file with name of your choice.
  • Run the script for eg: sh pdg.sh
  • Add your commit message & done. Code is pushed.
ans2human
  • 2,300
  • 1
  • 14
  • 29
1

Here's a simple bash script

#!/bin/bash
git add .
echo "Insert your commit message >"
read -r commit
git commit -m "$commit"
echo "Pull, Push, or Switch Branch >"
read -r answer
if [[ $answer == "pull" ]];
then
    git pull
    echo "Done with pulling, would you like to push your files (y/n)?"
    read -r pushing
    if [[ $pushing == "y" ]];
    then
        git push
    else
        exit;
    fi
elif [[ $answer == "push" ]];
then
    git push
elif [[ $answer == "switch branch" ]];
then
    echo "Enter the new branch name >"
    read -r branch
    git checkout "$branch"
fi

save this as a .sh extension then +x for execution permission

P0MS
  • 43
  • 6
1

If you are using Zsh on macOS, you can create functions in your ~/.zshrc file to automate the repetitive Git commands you mentioned.

To get started, open your terminal and execute the following command to open the ~/.zshrc file in a text editor:

open ~/.zshrc

This will open the ~/.zshrc file in the default text editor configured on your system.

Add the following functions to the file:

function gitinit() {
    git init
    git add .
    git commit -m "$1"
    git branch -M main
    git remote add origin $2
    git push -u origin main
}

function gitcommit() {
    git add .
    git commit -m "$1"
}

function gitpush() {
    git add .
    git commit -m "$1"
    git push origin main
}

Save the file and exit the text editor.

To apply the changes and make the functions available in your current terminal session, you can either restart your terminal or run the following command:

source ~/.zshrc

Afterward, you can simply use these functions to automate your Git workflow. For example:

gitinit "Initial commit" git@github.com:your-username/your-repo.git
gitcommit "Added new feature"
gitpush "Pushing latest changes"

These functions will execute the corresponding Git commands with the provided arguments.


Make sure to replace your-username and your-repo in the gitinit function call with your actual GitHub username and repository name.

Lev
  • 11
  • 3