git logo 2 colorGit is a powerful version control software. If you are unsure of what version control is, you’ve most likely done it before. If you have ever saved a file (ex. file.txt), made some edits, and saved a second copy (ex. file2.txt, filerevised.txt), you have used a basic form of version control. Git is great at automating that process along with having additional tools and functionality. Read on to get started with Git.

Get Started With Git

Once the installer is completed, open up your new GitBash or Terminal (depending on your OS). In the terminal, you should see your [email protected] name, a path to where you currently are, and a dollar sign. You should be ready to start version controlling your projects.

Create Username & Email Into Git

To get a little familiar with some syntax, Git commands are usually Git *command* *parameters*. To get you warmed up, you should configure your username and email. These are used to track who has modified the files for each commit. Enter the following commands on separate lines to configure your user information (replacing my information with yours).

git config --global user.name "Ryan Van Ess"
git config --global user.email "[email protected]"

Create Project Directory

After you’ve set your user info, you’ll want to create a project directory. Enter mkdir git-practice  to create a directory called git-practice. Next you’ll have to enter that directory by using cd git-practice/  . After you have entered your new directory, your file path, before the dollar sign, should have updated. Once verified, use git init to initialize a new Git project.

Create Files for Tracking

Now that you have a newly create Git repository, you need some files to track. Use cat >README.md  to create a markdown file called README. you will be prompted to add some content. Type the following: #My first time using git!# and hit CTRL (CMD) + c to exit. Your file has been created with some text inside.

To start tracking that file, use git add README.md to move your Read Me file from your working directory to your staging directory. The working directory consists of all of your files where you initialized git, whereas the staging directory is where your files go to be commited to the repository.  You can type git status to see the current status of your project.

Commit Your Files

Next you’ll want to commit that file to your local repository. There are many ways to commit your files. My favorite is git commit -m "Commit message" . So it’ll commit your file and attach a commit message all in one. A good rule of thumb for committing this way is to keep your message short. If it is your first commit of the project, I usually default to “Initial Commit”. Otherwise, the message should usually state what it is going to add to the project, ex “Adds method xyz to ABC class”.

Create Public Repository

After you have your first commit, you are going to need a place to remotely store your project. I generally recommend GitHub for public repositories, but there are many to choose from. If you don’t already have an account, sign up for one. After you’ve signed up, there is a plus sign in the upper right corner next to your user profile. Click on the plus sign then the “New Repository” option. One you give it name, hit the “Create Repository” button.

Pushing Files to Public Repository

You’ll want to copy the line that says git remote add origin ... Go back to your terminal and paste it. If CTRL+V (CMD+V) or CTRL+SHIFT+V (CMD+SHIFT+V) doesn’t work, click in the upper left corner of the terminal, go under edit, and click paste. The remote command is to set the git pointers to point towards your remote repository. The add parameter is to add the remote to the remote pointers list. The origin parameter is the alias of your remote pointer. You can name this anything you want, as long as you can remember. We will be using this alias later.

To push your files to the remote repository, use git push -u origin master. You may be prompted to enter your GitHub username and password. This will compress your files, then move then to where your remote points to. Origin, again, is your remote alias. Master is the master branch of your project. If you go to your GitHub repository and refresh your browser, you should see your files and recent commits.

If you need more of an in depth explanation or more options visit this Git Documentation.

 

See also Running PHP From Windows Command Line