Version Control – Intro to Git

In my previous article of this series, I discussed the importance of version control. I highly suggest reading that article before continuing if you have not already done so (unless you already have a good understanding of version control systems).

At this point, I hope we can all agree that version control is needed. It provides too much value to not utilize it. So, how do we go about actually using it?

Set Up Repository

A repository is essentially just a grouping of files, their version history, and commit messages all hosted on a server. This server can either be hosted internally (typically an Enterprise-level solution) or hosted on a service like GitHub or BitBucket (comparison of both). I don’t want to go into how to actually set up your own server. That is more advanced functionality you won’t need to know at this point. However, let’s take a look at setting up a repo (short for repository) up on GitHub.

The first thing you want to do is sign up for an account on GitHub. GitHub offers free unlimited repos and collaborators as long as you have no private repositories. A collaborator is simply a user who can work on the files inside your repository. The difference between the two types of repo:

  • Public – This repository is open to the world for anyone to see. It can still be locked down on who can actually edit it, but the code is available to be looked at. This is perfect for an open source solution.
  • Private – This repository is closed from anyone who doesn’t explicitly have access to it. This is required in most environments where the code is proprietary and you don’t want anyone to be able to just look at what you have done.

In the first step, you just want to enter some basic information.

In the second step, you will need to select the plan you want. I am going the free route (which means my repo will be public), but you may want to go a paid option to provide a private repo for your organization.

As soon as you sign up, you will be taken directly to the GitHub dashboard.

Assuming (since you are following along with this article) that you are new to Git, I would highly suggest going through the GitHub Bootcamp. Downloading and installing Git is something that is universally the same. Essentially, that tutorial will walk you through installing Git on your machine so that it will be able to run git commands through your terminal or command line. Once you have that set up, let’s go ahead and create a repo.

On this page, you can enter a repo name. Typically, you will want to keep it short. I suggest adding a description. You can then specify if you want a private repo (only available on the paid plans) or a free repo.

You will immediately be taken to a page that will detail out how to go about creating your repo. However, before we do that, we need to make sure our code structure is set up. Typically, you will want to do this through an IDE. In this demo, I am using MavensMate, but you could also be using the Force.com IDE. If you are absolutely opposed to an IDE, you could also use the Force.com Migration Tool. Essentially, you just need a mechanism for getting code down to your base machine. There are guides on how to do this using any of the available mechanisms, so I will skip to the part about actually getting your repo set up. If you are looking at other migration tools such as https://www.cognillo.com/sharepoint-migration you will need to conduct personal research on how that will work for your system.

First thing you want to do is cd to the proper directory in which your files are stored.

[shell]
Jesses-MacBook-Air:~ jessealtman$ cd Documents/Git-Tutorial/Git-Tutorial/
[/shell]

Next, you will want to initialize your repo with git init.

[shell]
Jesses-MacBook-Air:Git-Tutorial jessealtman$ git init
Initialized empty Git repository in /Users/jessealtman/Documents/Git-Tutorial/Git-Tutorial/.git/
[/shell]

If you are really interested, you can learn more about the .git directory. At any time, you can check the status of your repo by utilizing git status.

[shell]
Jesses-MacBook-Air:Git-Tutorial jessealtman$ git status
# On branch master
#
# Initial commit
#
# Untracked files:
# (use “git add …” to include in what will be committed)
#
# Git-Tutorial.sublime-project
# Git-Tutorial.sublime-workspace
# config/
# src/
nothing added to commit but untracked files present (use “git add” to track)
[/shell]

At this point, we are ready to add these untracked files to our repo. To do that, we will run git add.

[shell]
Jesses-MacBook-Air:Git-Tutorial jessealtman$ git add .
[/shell]

Notice how git add has a final parameter. By utilizing the . command, I went ahead and marked all of the untracked files as ready to be committed. Let’s run git status again.

[shell]
Jesses-MacBook-Air:Git-Tutorial jessealtman$ git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
# (use “git rm –cached …” to unstage)
#
# new file: Git-Tutorial.sublime-project
# new file: Git-Tutorial.sublime-workspace
# new file: config/.apex_script
# new file: config/.debug
# new file: config/.describe
# new file: config/.local_store
# new file: config/.session
# new file: config/.settings
# new file: src/package.xml
#
[/shell]

Notice how all of our files are now set to be committed. We can now go ahead and run git commit to add them to our local repo.

[shell]
Jesses-MacBook-Air:Git-Tutorial jessealtman$ git commit -m “Initial commit of the Git-Tutorial repo”
[master (root-commit) 123963d] Initial commit of the Git-Tutorial repo
9 files changed, 564 insertions(+)
create mode 100644 Git-Tutorial.sublime-project
create mode 100644 Git-Tutorial.sublime-workspace
create mode 100644 config/.apex_script
create mode 100644 config/.debug
create mode 100644 config/.describe
create mode 100644 config/.local_store
create mode 100644 config/.session
create mode 100644 config/.settings
create mode 100644 src/package.xml
[/shell]

Notice how all of these files have been added. It is important at this point to note the distinction between Git and most version control systems. When we commit here, we are only committing our code to our local repository. In Git, there is a concept of a local and remote repo. It provides you the ability to commit continually and as often as you possibly want without effecting anyone else developing on the repo. We now need to set up our connection to the remote repository. To do that, we will use the git remote command.

[shell]
Jesses-MacBook-Air:Git-Tutorial jessealtman$ git remote add origin https://github.com/jessealtman-git-test/git-tutorial.git
[/shell]

Note you will have to change the URL to the remote repo you set up. We can now go ahead and push directly to remote. Note, we are only pushing directly before a pull because this is the very first commit. Typically, we will want to pull first. I will address that in the next article of the series. We can use the git push command to push our initial commit to remote for anyone else to pull down.

[shell]
Jesses-MacBook-Air:Git-Tutorial jessealtman$ git push -u origin master
Username for ‘https://github.com’: jessealtman-git-test
Password for ‘https://jessealtman-git-test@github.com’:
Counting objects: 12, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (10/10), done.
Writing objects: 100% (12/12), 3.26 KiB | 0 bytes/s, done.
Total 12 (delta 0), reused 0 (delta 0)
To https://github.com/jessealtman-git-test/git-tutorial.git
* [new branch] master -> master
Branch master set up to track remote branch master from origin.
[/shell]

Note, you will be prompted for your username and password. At this point, you have officially created a repo and put it up on GitHub! The full output looks like:

[shell]
Jesses-MacBook-Air:~ jessealtman$ cd Documents/Git-Tutorial/Git-Tutorial/
Jesses-MacBook-Air:Git-Tutorial jessealtman$ git init
Initialized empty Git repository in /Users/jessealtman/Documents/Git-Tutorial/Git-Tutorial/.git/
Jesses-MacBook-Air:Git-Tutorial jessealtman$ git status
# On branch master
#
# Initial commit
#
# Untracked files:
# (use “git add …” to include in what will be committed)
#
# Git-Tutorial.sublime-project
# Git-Tutorial.sublime-workspace
# config/
# src/
nothing added to commit but untracked files present (use “git add” to track)
Jesses-MacBook-Air:Git-Tutorial jessealtman$ git add .
Jesses-MacBook-Air:Git-Tutorial jessealtman$ git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
# (use “git rm –cached …” to unstage)
#
# new file: Git-Tutorial.sublime-project
# new file: Git-Tutorial.sublime-workspace
# new file: config/.apex_script
# new file: config/.debug
# new file: config/.describe
# new file: config/.local_store
# new file: config/.session
# new file: config/.settings
# new file: src/package.xml
#
Jesses-MacBook-Air:Git-Tutorial jessealtman$ git commit -m “Initial commit of the Git-Tutorial repo”
[master (root-commit) 123963d] Initial commit of the Git-Tutorial repo
9 files changed, 564 insertions(+)
create mode 100644 Git-Tutorial.sublime-project
create mode 100644 Git-Tutorial.sublime-workspace
create mode 100644 config/.apex_script
create mode 100644 config/.debug
create mode 100644 config/.describe
create mode 100644 config/.local_store
create mode 100644 config/.session
create mode 100644 config/.settings
create mode 100644 src/package.xml
Jesses-MacBook-Air:Git-Tutorial jessealtman$ git remote add origin https://github.com/jessealtman-git-test/git-tutorial.git
Jesses-MacBook-Air:Git-Tutorial jessealtman$ git push -u origin master
Username for ‘https://github.com’: jessealtman-git-test
Password for ‘https://jessealtman-git-test@github.com’:
Counting objects: 12, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (10/10), done.
Writing objects: 100% (12/12), 3.26 KiB | 0 bytes/s, done.
Total 12 (delta 0), reused 0 (delta 0)
To https://github.com/jessealtman-git-test/git-tutorial.git
* [new branch] master -> master
Branch master set up to track remote branch master from origin.
[/shell]

Notice, this process is very similar to what GitHub actually suggests on the page directly after creating your repo.

At this point, you will be able to view your repo inside GitHub!

In the remainder of this series, I will discuss branching and adding more of your code to the repo. I will even discuss the proper way to add your other metadata (such as custom objects). Using those methods, you will be able to version every single aspect of your organization, providing structure and security.

Good luck!

Advertisement

Go to Smartblog Theme Options -> Ad Management to enter your ad code (300x250)

Comments are closed.