Overview
This is a step-by-step process for setting up Git repositories, connecting them to Bitbucket, and creating a script to automatically push changes to Bitbucket on a nightly basis.
1. Setting Up a Local Git Repository
-
Launch Git GUI
-
Create the Repository:
-
In the Git GUI, click on Create New Repository.
-
Browse to and select the folder where you want the repository to be created.
-
Click Create.
-
2. Connecting the Local Repository to Bitbucket
-
Clone the Bitbucket Repository:
-
Log in to your Bitbucket account.
-
Navigate to the repository you want to connect.
-
Click Clone.
-
Ensure https is selected, not ssh.
-
Copy everything that comes after
git clone.
-
-
Add the Bitbucket Repository as a Remote:
-
In Git GUI, go to the Remote menu and select Add.
-
For Name, use
origin(or any preferred name). -
For Location, enter the URL of the Bitbucket repository (e.g.,
https://username@bitbucket.org/username/repository_name.git). -
Click Add.
-
-
Configure Repository Access Tokens:
-
For each repository, you need to create an access token in Bitbucket. Follow the instructions here: Create a Repository Access Token.
-
Inside of the directory, from the “web1” command line, EDIT the following commands to update your repository settings to use the access token instead of your personal credentials:
git remote set-url origin https://username:access_token@bitbucket.org/username/repository_name.git
git config user.email "bot_email_address"
-
3. Automating Nightly Backups
-
Create the Backup Script:
-
Reviews scripts in “D:\scripts” for structure and setup. There is a single Git Shell script (.sh) for each repository so they can be run individually.
-
Create a new file named
push_XXXX.shto match the repository 1:1. -
Copy the “git” lines from another script, adjusting the folder name to the folder connected to the repository on each line.
-
The script adds all types of code files we want to backup, commits (all new files, all new folders, etc.) and then pushes.
-
The -C and full path “knows” to run in the .GIT setup in that folder to the right Bitbucket connection already setup.
-
-
Test the Script:
- Double click to launch the script to ensure it works.
-
Add the Script to “ALL”
-
Add a new line to
push_ALL.shwith the new script. The “.” is critical. -
This will then be included in the already setup nightly process (more below)
-
-
Subscribe on BitBucket
-
If you wish to get nightly notification emails when each is run, subscribe to them on Bitbucket for each repository.
-
This is under the three dots upper right, Manage Notifications, and check Commits.
-
4. How the Nightly Process is setup
-
Launch Windows Task Scheduler
-
“Git Push Web Code to BitBucket” runs nightly at 11pm
-
It launches
D:\scripts\push_ALL.batwhich is a Batch file that properly runs the Git Shell script via the Git command line.
-
