Let’s quickly chat about Git and Github. And then we’ll get to those lovely Git Branches.
Now I learned a while while ago, just how important it is to use Git. It’s a form of version control that developers use for their code. It’s important because they can keep track of when certain changes were made, and (god forbid) they can go back to an earlier version of their code if an error occurs with a change. Also Github is important because it acts as a backup for any code projects, while making it really easy to share projects with team members or just the general public.
Unfortunately I haven’t been using Git for about 2 months. First because I am just out of practice and haven’t been using the command line like I should be (ideally I’d be using it daily just to stay familiar..). Second I have not been on my computer, since the screen is broken. I have been on my husband’s home computer (which has a dual monitor set up, it’s dope). So I needed to download Git and reset up everything on his computer. So no excuses! I’m doing it now!
Now let me introduce a project I’ve been working on and why I would’ve use Git Branches from the beginning had I understood it.
Most recently I am developing a “welcome slide.” I want this to look very professional, to fill up the whole screen, and ideally have all dynamic content, meaning that I won’t have to go in and update it every week. I’m thinking it’ll post the current date, a current scripture reading, current mass times, etc. This same design is going to be used in the parish office, the church office, and the gym, so once I get something I’ll like, I’ll start tweaking it and adding different functionality for different purposes.
While developing this “welcome slide,” I have been creating different versions of my slideshow project, so that I can make changes without messing with my project code that already works. This is basically exactly what Git Branches are for! Instead of creating a new file every time and copying and pasting when something I works, I could create a new branch in Git, experiment until I have code I like, and then merge the branches to commit that code to the main project!
First I’m going to create a repository (since I haven’t been using Git already–oops!)
Create Repo in Git
- Using command line (terminal-Mac) navigate to the correct directory
- Initialize repo: type git init in terminal to create an empty Git repository (/sites/hs/slideshow)
- type git status just to make sure you’re in the right place and to see all the files available to commit
- Add each file individually by name with git add (+insert name!) OR use git add . to add everything listed in red
- I’m going to add individually because I only want to save my original slideshow project
- type git status to confirm you’ve add everything you want for this commit
- Time to commit changes!
- type git commit -m “insert your own comment here!” obviously writing your own appropriate comment here
- type git status just to make sure your files were committed. My working tree will not be clean since I did not commit all of my files
Now that my repo is set up, I want to start working on my more experimental code. So I am going to create a branch to do so.
Create a branch in Git
- Create and check out a new branch on the command line
- type git checkout -b welcome (welcome is just the name of my new branch—feel free to sub your own name!)
- type git branch just to confirm what branch you’re working in
- Use open . to open this branch in finder
- Now you can make whatever changes you want to your files and it will only affect this branch and not the master branch
- type git branch whenever you need to confirm you’re in the right branch
- use git checkout master when you need to switch back to the master or git checkout welcome to switch to the welcome branch
- Whenever you’ve made a significant change or are reaching a stopping point, remember to commit changes!! That’s the whole point of this!
- remember to first git add the files that have changed
- then git commit -m “comment about change” to commit the change
- and for peace of mind git status to make sure your working tree is clean 🙂