preface

When you go to your Github home page, you’ll see your submission record. If you didn’t submit a record on a given day, the small box will show gray on that day. Obsessive-compulsive disorder, I feel uncomfortable every time I come in and look at it, thinking that I remember to submit something every day, and strive to be like Ruan Yifeng God, every day have submitted records.

Ruan Yifeng submits records to Github

However, as human beings, they will forget to submit when they are busy one day. Therefore, I wonder if I can set cron and customize git command on my Ari cloud server (Linux system) to realize automatic submission at fixed points every day.

Step 1: Clone my project

Different clone modes lead to different verification modes, and the corresponding secret exemption modes are also different. In simple terms, HTTPS is login free by remembering the account password, and SSH is login free by verifying the generated key.

  1. HTTPS cloning
HTTPS cloning

  1. SSH cloning
SSH cloning

If you have cloned the project and don’t know which way, you can do it:

git remote -v
Copy the code

If so:

origin https://github.com/tywei90/git-auto-commit.git (fetch)  
origin https://github.com/tywei90/git-auto-commit.git (push)  
Copy the code

So HTTPS;

If so:

origin  [email protected]:tywei90/git-auto-commit.git (fetch)  
origin  [email protected]:tywei90/git-auto-commit.git (push)  
Copy the code

So SSH.

Changing the cloning method is also simple:

HTTPS — > SSH git remote set-url origin [email protected]:tywei90/git-auto-commit

SSH – > HTTPS git remote set – origin url https://github.com/tywei90/git-auto-commit.git

Step 2: Non-secret login

There are two cryptogram-free login Settings for the above two clone project approaches.

1. Password free (HTTPS clone)

cd git-auto-commit/.git
vim config
Copy the code

Add the following code at the end of the config file:

[credential]  
    helper = store
Copy the code

Save, enter the account password once after the second time will remember the account password

2. Public and private keys (SSH clone)

2.1 Generating public and Private Keys

Check SSH key of the machine:

cd ~/.ssh 
ls
Copy the code

If “No such file or directory” is displayed, you are using Git for the first time

Run the ssh-keygen command to generate an SSH key.

ssh-keygen -t rsa
Copy the code

After entering the preceding command, press Enter to generate the SSH key. After the SSH key is generated, you can view related files in the ~/. SSH directory. Generally, the SSH key contains id_rsa and id_rsa.pub, which indicate the generated private key and public key respectively.

2.2 Copying public Keys to Your Github

In the. SSH directory, run cat id_rsa.pub to copy all the public keys

Click github’s profile picture, select Setting from the drop-down menu, and click SSH and GPG Keys from the menu on the left of the page. Then click the green button New SSH Key in the upper right corner of the New page. Fill in the title value and paste the copied public key content into the key input box for submission.

2.3 Test link github

Enter the following command:

SSH - t [email protected]Copy the code

SSH: Could not resolve hostname \342\200\223t: Name or service not known

ssh -t -p 22 [email protected] 
Copy the code

-p Changes the server port number to 22. Enter yes/no when prompted. Enter yes and press Enter. However, the error is still reported, and then I search again and execute the following code:

ssh [email protected]
Copy the code

Hi ** You’ve successfully authenticated, but GitHub does not provide shell access. The connection is successful. You can all try it.

Step 3: Set up CRON to automatically submit tasks periodically

The add.js in the project is used to modify recordings.txt, and each execution appends the current time to the end of recordings.txt. Then let Git commit automatically. The next key is the cron Settings, for Linux I am not familiar with or spend a little time, here directly paste the cron Settings. Run crontab -e to enter cron editing and paste the following code:

00 12 * * * cd/ home/git - auto - commit && git pull && / root/NVM/versions/node/v6.6.0 / bin/node. Add js && git commit-a -m 'git auto commit' && git push origin master && git log -1 | mail -s "git auto commit successfully!" [email protected]
Copy the code
  • 00 12 * * * means that the following orders are executed at 12:00 every day.

  • / root/NVM/versions/node/v6.6.0 / bin/node is the node binary executable absolute paths, not directly write node command, can’t identify. How to find your node execution directory is simple, in fact, to execute which node.

  • ‘Git auto Commit’ is the comment for each commit and can be used as desired

  • git log -1 | mail -s “git auto commit successfully!” [email protected] is the latest git commit log as the email content, “git auto commit successfully!” As a subject, send an email to [email protected]. Of course, this is optional. I want you to send me a confirmation email after each automatic submission. By observing whether the date value in the email is the current time, you can judge whether the automatic submission is successful. If you want to implement this feature, you will need to configure the Linux mail sending Settings. I’ll write that later. The main attention should be paid to ali cloud server on mail 25 port restrictions, pit!

Step 4: Use shell scripts to batch fill up the previous submission records

Github’s own home page displays the previous year’s commit records by default. How to replace the previous year’s commit records? Fortunately, github’s commit record time is based on the COMMIT time, so we can change the time of our computer, and then commit. I use centos7 as an example. The command to change the time may be different for different Linux versions.

4.1 Change the system time to the end of the time period to be made up

For example, if you want to change the time segment 2018-01-01 to 2018-01-31, you need to change the system time to 2018-01-31. The script is as follows:

timedatectl set-time 'the 2018-01-31 13:00:00'
Copy the code

4.2 Go to the project directory and run the loop script

To determine the number of days we want to modify, 2018-01-01 to 2018-01-31 is 31 days, we pass this parameter on the command line

cd git-auto-commit
screen -d -m -L sh loop.sh 31
Copy the code

Here, the screen-d -m -l command can background the task we are executing, so that the script can exit the server connection without terminating. You can refresh your Github homepage to see if all the gray areas have turned green.

Afterword.

At this point, github auto-commit Settings are complete. Let’s take a look at the results:

My Github submission record

And then every day at 12 o ‘clock I get the following email:

Git automatically submits mail

Welcome to star learning communication: making the | my blog address

(after)