NOTE, 1 October 2020 the label ‘master’ was replaced by ‘main’ for the default repo branch.
Introduction
This post covers git version control with a shared master repository on local machines. The shared master is not used for active development, but is more of a container for holding versions that stem from clones. The development only happen in the clones and the master just receives pushes and send out pull requests from clones.
This post is mainly based on different parts from the tutorial series Learn Git with Bitbucket Cloud. Links to relevant pages are given in different sections below.
Prerequisites
You need to install git for command line use as outlined in the parallel post on Local git control.
Create directory with git control
Create a new, empty, directory. To use the Terminal first change directory (cd) to the parent folder under where you want to create the new directory.
$ cd path/to/parent/directory
You can also just type $ cd in the Terminal window, then open a Finder window, navigate the the parent folder and drag the directory icon of the parent to the Terminal window.
You are now ready to create a new directory, that will be converted to a shared master repository in the next section. By convention, shared repos end with .git as an extension of the directory name. For this tutorial, you are going to create a shared repo, and if you want to follow the convention you should name it, for instance git-test-dir.git.
Make sure the Terminal points towards the parent directory, and create the new directory with the mkdir command:
$ mkdir git-test-dir.git
cd to the newly created directory:
$ cd git-test-dir.git
Git init
To create a shared master you must add the parameter --bare to the init command:
$ git init --bare
The response will be something like
Initialized empty Git repository in path/to/git-test-dir.git
For details on Git init and the --bare option.
If you now check the content of your directory by listing (ls) its content:
$ ls
You will see a list of files and folders:
HEAD config description hooks info objects refs
In an “ordinary” repo, all of these files and folders are hidden (under the folder .git) repo, but not so in a --bare repo used as a master container.
Add a readme.md
Add a README file to the shared master repo:
$ pico README.md
Shared master repo for project on ...
Hit [ctrl]+[X] to exit pico and save the edits by pressing Y when asked.
Clone I
Change directory to the parent folder of your newly created repo:
$ cd..
Now clone the master with the command git clone path/to/master path/to/clone, with either relative or absolute paths. As your terminal window is in the parent folder of your shared master you can create a parallel clone like this:
$ git clone git-test-dir.git/ git-test-dir_dev
Cloning into 'git-test-dir_dev'...
warning: You appear to have cloned an empty repository.
done.
Link to Bitbucket Atlassian in depth page on git clone command.
Create document in clone
Create a document in your clone repo, for instance a markdown (.md) md file. I have created a file named Chapter.md, with the following content:
# Chapters in book on climate change evidence from Remote Sensing
## Introduction
## The sun
## Earth's energy balance
## Physical feedbacks
### Albedo changes 1970 - 2020
Once you have created the document, make sure your terminal window is in your cloned repo and type:
$ git status
On branch master
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
Chapters.md
nothing added to commit but untracked files present (use "git add" to track)
Stage and commit
As in the previous post on Local git control, you have to stage and then commit any changes before you can pull or push them. First stage the new file:
$ git add Chapters.md
Then commit all (-a) while also giving a message (-m):
$ git commit -am "initial commit"
[master (root-commit) 5e660be] initial commit
1 file changed, 11 insertions(+)
create mode 100644 Chapters.md
More details on from the Atlassian BitBucket pages on Saving changes and Git add and Git commit
Git Push
Your edits in the clone are now staged and commited, and you can push them to the shared master:
$ git push
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Delta compression using up to 4 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 355 bytes | 355.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To /path/tp/git-test-dir.git/
* [new branch] master -> master
Clone II
Let us create a second clone of the shared master. This time we can use absolute paths and ignore where the terminal window is pointing:
$ git clone path/to/git-test-dir.git/ path/to/git-test-dir_share
Cloning into 'path/to/git-test-dir_share'...
done.
This time the returned message does not state that we cloned an empty repository. If you check the content of the new clone (“git-test-dir_share”) you will see that it contains the file you created and edited in the development clone and then pushed to the shared master.
Resources
Learn Git with Bitbucket Cloud
Pro Git - Everything you need to know about git by Scott Chacon and Ben Straub (20200219).