For the Love of Git!

Jay Wen
3 min readMay 12, 2020

--

As developers, version control is one of the most important things when it comes to working on a project. Git allows users to be able work collaboratively and track their code for any file changes.

Here is a tutorial for starting a new repository and the breakdown of what each git command does.

General rule of thumb:

  • Make any new changes in a separate branch and NOT on the master.
  • Commit more often than you think. Every 5 lines of code or every 15–20 minutes
  • Write descriptive commit messages. You will be glad you did so in case you have to go back. Ie — “adds new feature”, “removes spaghetti code” or “fixes styling”.

Create a Repository on GitHub:

  • To create a remote repository on GitHub, you will go to github.com/new and log in.
  • Enter a name for the repository and for our case we’ll call it “demo-repo” but you can name it whatever you like.
  • Leave the default options as they are and click “Create repository” button.
  • After clicking the button, copy the SSH link of your repo. We’ll be using this link later.

Git Workflow:

  • In your terminal, create a folder for your repo. On the command line you can type: “mkdir demo-repo” . “Mkdir” means make directory.
  • Then cd into the new repo by typing: “cd demo-repo”. Now you should be inside of this folder.
  • Create a README file by typing: “touch README.md” and you’ll be able to see it in the file tree of your editor. The command “touch” allows you to create a new file and the second part is what you want to name your file. Generally in the README file you’ll want to have a brief description of the project and technologies used, what version and how to run the project. But for our case you can say, “This is the readme file for my demo-repo.”
  • Next in your terminal type: “git init” and this will initialize an empty Git repository.
  • Now we want to add the readme file that we just created. Type “git add README.md”. This command tracks the file and specifically we are tracking the README.md file.
  • We can commit our file by typing: “git commit -m “first commit”. This is the first commit for our local repo.
  • Now we’ll set up the destination of where we want to push our code to the remote repo. In the terminal type: “git remote add origin [SSH url of your remote git repo]”.
  • Now that we’ve set up the destination, we can push the code to the repo. Type: “git push -u origin master”. The “-u” tells Git to save the remote repo as the default push destination. “Origin” refers to the repository name and we are pushing to the remote repo’s default branch master.

Now you’ve successfully connected your local and remote repos and can start working on your new project. Remember to create a new branch for new features that you’re working on.

  • To create a new branch type: git checkout -b [development]

Remember to commit often.

  • In your terminal type “git add .” the “.” indicates that it will track all the files you’ve worked on
  • Then add a commit message: “git commit -m “adds new function”.
  • Finally: “git push” and this will push to the branch you created… for us we named it “development”.

Now let’s say this new feature works perfectly and you want to merge it with master.

  • In the command line type: “git checkout master”, which will take you back to the master branch
  • “git merge development” which will merge all of the files from development into the master branch. Now the master branch is updated with the development branch.
  • Finally you can commit your changes from master. You’ll follow the same steps with “git add .”, “git commit -m “message”, and “git push”.

I hope this tutorial helps! There is a lot happening under the hood when we type in these commands (that we can’t see) but I hope this clarifies the steps on the git workflow. :)

--

--

Jay Wen
Jay Wen

Written by Jay Wen

Hey! I’m a full stack software engineer. Here’s where I document my technical learning and any tips/skills I can share as I continue to learn. :)

No responses yet