Git 2: Initial Setup

logo%402x.jpg

First-Time Setup

Refer to the Git book to install Git, then let’s get started! According to this page:

Now that you have Git on your system, you’ll want to do a few things to customize your Git environment. You should have to do these things only once on any given computer; they’ll stick around between upgrades. You can also change them at any time by running through the commands again.

Git comes with a tool called git config that lets you get and set configuration variables that control all aspects of how Git looks and operates.

The first thing you should do when you install Git is to set your user name and email address. This is important because every Git commit uses this information, and it’s immutably baked into the commits you start creating:

git config --global user.name "John Doe" git config --global user.email johndoe@example.com

Again, you need to do this only once if you pass the --global option, because then Git will always use that information for anything you do on that system. If you want to override this with a different name or email address for specific projects, you can run the command without the --global option when you’re inside that project’s working directory.

You can configure the default text editor that will be used when Git needs you to type in a message. If not configured, Git uses your system’s default editor.

git config --global core.editor vim

If at any time you wish to check your configuration settings enter:

git config --list

You may see keys more than once, because Git reads the same key from different files ([path]/etc/gitconfig and ~/.gitconfig, for example). In this case, Git uses the last value for each unique key it sees.

The Three Operations

Recall from the previous post there are 3 major Git operations: modifying, staging, and committing.

Working tree, staging area, and .git directory

Working tree, staging area, and .git directory

I will proceed to take you through a concrete example of all three operations.

Modifying

To start, select any directory — say, for example, “project1” — and enter at the prompt:

mkdir project1
cd project1
git init

This creates an initial version of our repository — meaning a new subdirectory is created named .git inside the project1 directory that contains all of your necessary repository files — a Git repository skeleton. At this point, nothing in your project is tracked yet. See Git Internals for more information about exactly what files are contained in the .git directory you just created.

Now add a file to your project1 directory and the edit it with your favorite editor. Mine is gvim but there are many others. Editors are outside the scope of this document, but I do touch on them in a the FAQs. Refer to your individual editor’s documentation

touch proj1.py
chmod 775 proj1.py

What this does is:

  • create a file called proj1.py

  • chmod the file with the appropriate permissions

Add the following to proj1.py:

print(“Hello World”)

Then at the prompt enter:

python proj1.py

Which should output:

Hello World

Now we are ready to move on to staging.

Staging

Enter (at the prompt):

git status

It should output something like the following:

On branch master
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
proj1.py

nothing added to commit but untracked files present (use "git add" to track)

This tells us that

  • We are on the master branch

  • There are no commits yet

  • We have 1 untracked file that needs to be staged

  • Suggests we use git add to start tracking the file

Let’s do what git suggests and stage the file with

git add proj1.py

Now git status should yield:

On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: proj1.py

We have successfully staged the file and are ready to commit!

Committing

Enter at the prompt:

git commit -m "Added Hello World file."

-m is an optional argument that tells git not to open the default editor when executing the commit. If you did everything correctly up till now you should see:

[master (root-commit) 338caa5] Added Hello World file.
1 file changed, 1 insertion(+)
create mode 100755 proj1.py

Now the SHA 338caa5 will be different, and that’s as it should be. This is Git telling us:

  • the commit message

  • files changed and inserted

  • Git created a regular exectable file (see stackoverflow)

If we type git status now we should get:

On branch master
nothing to commit, working tree clean

Bravo! You have just done your first Git commit!

Best Practices

Only One Module Per commit

Consider making 1 commit per module as here.

ArduPilot generally employs a 1 commit per module approach.

ArduPilot generally employs a 1 commit per module approach.

The modules in the above example are:

  • Tools/autotest

  • Copter

  • AP_Baro library

  • hwdef

  • AP_HAL library

  • and so on…

Make Commit Messages Descriptive

See self-documenting code.

Add Binary Files To .gitignore

See here.

Credits

https://linuxfromanoobie.wordpress.com/2018/04/15/git-starting-off/

https://opensource.com/article/20/7/git-best-practices

https://deepsource.io/blog/git-best-practices/

Next Installment

Next time we’ll take a look at collaboration via GitHub.

Previous
Previous

Git 3: Collaboration on GitHub

Next
Next

Git 1: Why Git?