DEV Community

Daniel Zaltsman
Daniel Zaltsman

Posted on

How does one git good?

If you haven't heard by now, git is what developers use to keep track of each version of their projects. It is an extremely powerful tool designed to let you monitor each change, addition, and subtraction done to it.

I am by no means an expert in git, but there are a few basic commands I always use for my projects. Here are the few that I have deemed integral to my work flow:

git clone

When you're starting a project, and you need to connect your local directory to your remote repository(github for example), first copy the SSH key from your github repo first. Then, while you're inside your local project directory in the terminal, you type this command:

git clone "ssh key here"

Now your project is linked to the remote repository. Now whenever you want to keep track of the differences in your code between now and the time you last edited your code, you can!

git init

If you've already started a project locally and you didn't want to clone an existing project, you can start tracking your files with git init. Access your local directory in the terminal and type:

git init

This will create an empty git repository for you. But in order for you to start tracking your untracked files, we need to give a few extra commands.

git add .
git commit -m "Initial commit."

Git add, status, and commit

Now that your git is tracking files, we can actually get to working on our code. Every so often, you'll want to save a version of your code by adding the file changes to that version, and then committing it to the specific version that you're saving. To check what you've changed so far type this into the terminal:

git status

You should see a bunch of lines in red detailing what changes were made to your project. If you're satisfied with all of these changes, you can use the add command:

git add .

The dot means you would like to include all of the changes. If you type git status again, you will see those same lines that were in red are now green, and that those files are staged and ready to be committed. Once you're ready to commit these files, you will use the commit command as well as writing a simple message along with it to describe your changes

git commit -m "Initial commit."

Now you have a version of your project saved in its current state that you can go back to if you made a project breaking mistake.

Git push

If you wanted to push all your code to your github(which you probably do), you can use the git push command. If you haven't connected your local directory to your github repository, use this command:

git push origin master

This creates a branch called master. Origin is the remote server that you are pushing to. In this instance, it is the github repo that you cloned. If master branch already exists, it updates it.

If you didn't initially connect your local directory to an existing repository, there is a command that will attach it so that whenever you push to "origin master", origin will be the remote server that you referenced:

git remote set-url origin new.git.url/here

I advise against doing it in this order, as git clone handles everything for you from the beginning.

Top comments (2)

Collapse
 
fluffynuts profile image
Davyd McColl

good article; a suggestion for git add though:

git add . adds all new and changed files from the current folder downward. If you'd like to do that for the entire repo, from any folder, try: git add -A :/, which means "hey git, please add all new and modified files, as well as deleted files (-A), for the next commit, for this entire repository (:/)". It's my goto (:

Collapse
 
danimal92 profile image
Daniel Zaltsman

Ahhh, interesting. Thanks for the feedback!