How to Get Good at Git

A simple guide to get up and running with Git on the command-line

Endlessly renaming your files is a solid process for a 4th grader learning Microsoft Word, but you’re an engineer, dammit! We have the technology for greater version control, however, to the newly “git-nitiated” it can be intimidating: so they often ignore, or delay, accepting it.

In this post we’ll accomplish the following feats:

  • Initialize an empty repo and build it from scratch
  • Create an SSH key and add it to our account
  • Make changes to our codebase and sync them with origin

Better git your priorities in order…

Gitting our account ready

I prefer Gitlab because it was what I was exposed to first, and they were the first big name to offer unlimited free private repositories, which Github now offers as well.

After we’ve created an account, we’ll have a user homepage that looks something like this: https://gitlab.com/ericmg. This URL is how we’ll connect a repository to our account.

To create a new repo we simply go to the directory we desire as root and run:

>$ git init
Initialized empty Git repository in /Users/egirouard/workspace/medium/.git/

This produces a directory called .git which contains all the information necessary to version and control your files. Let’s make some files so Git has something to track:

>$ echo "Hello Medium, Welcome to my Repo" > readme.txt

Now that there’s a file in the directory, checking the status of our repo should yield results.

>$ git status
On branch master
No commits yet
Untracked files:
(use "git add ..." to include in what will be committed)
readme.txt
nothing added to commit but untracked files present (use "git add" to track)

The output from `git status` shows us the status of our repo/branch: We’re on branch master, with no commits, and one untracked file.

Git separates file additions and changes into three categories:

Untracked files: New files that Git has not been told to track previously.

Working area: Files that have been modified but are not committed.

Staging area: Modified files that have been marked to go in the next commit.

To have Git begin to track changes to our readme.txt file, we need to add it.

>$ git add readme.txt
>$ git status

On branch master
No commits yet
Changes to be committed:
(use "git rm --cached ..." to unstage)
new file:   readme.txt

Now, readme.txt has been added to our “working area”. To stage it, we must commit the file. Commits are accompanied by a message, so other developers (or ourselves later on) reading the project understand the logic or reasoning behind the change/addition. Commits are also searchable, so write strong, relevant messages.

However, If you tried to commit now, Git will complain that it doesn’t know who you are!

>$ git commit -m "Added Readme file to Project"
*** Please tell me who you are.
Run
git config --global user.email "[email protected]"
git config --global user.name "Your Name"
to set your account's default identity.
Omit --global to set the identity only in this repository.
fatal: unable to auto-detect email address (got '[email protected](none)')

Commits have authors, the name and email of the developer who made the commit. In order for Git to work properly, we need to provide it with this metadata, as well as provide it the means to connect to an origin, in this case, our Gitlab account.

Git’s messages do a very good job of pointing you in the right direction, we see the commands to run right from the console output above, and we can append `— global` if we want to set these values for the whole machine, rather than this single repository (a good idea if its a personal computer).

>$ git config user.email "[email protected]"
>$ git config user.name "Eric Girouard"

Now we can commit:

>$ git commit -m "Added Readme file to Project"
[master (root-commit) d7962a8] Added Readme file to Project
1 file changed, 1 insertion(+)
create mode 100644 readme.txt

Before we push this commit, we need to show ET how to phone home. Git needs a reference to its origin, the term for the remote repository the project was originally cloned from. However, if we create a project from scratch as we just did, we’ll need to construct our nascent repository a permanent home.

read original article here