I was recently talking to a longtime friend of mine who works in Silicon Valley and I was astonished to learn that many developers don’t actually know how to use Git from the command line. I know there are a couple of tools out there like Tower that are supposed to make things easier, but I find them sort of complicated actually. I bought a Tower license around 2 years ago and probably have used the app maybe 3 times. If anything, I use the Git integration built into Android Studio, but most of the time I’m using Git from the command line, and you should too. Most if not all Macs and Linux machines should come with Git already installed, if you don’t have it go ahead and install it here.

Prerequisites: A computer with Git installed, basic knowledge of Git, and basic knowledge of Unix commands

I have organized this tutorial by first stating what I want to do, and following that with a code example.

1. I have a project that I would like to start using git with. From within the root directory of that project do:

git init

Notes: This initializes the current directory to begin tracking changes with Git.

2. I have added a new file to my project, I would like to register it to my Git project to begin tracking changes.

git add myfile.txt

Notes: Per standard unix syntax, you can add all files in the current directory with git add .. Also, remember to perform a git add after making changes each time before committing.

3. I want to see all of the changes I have made to my Git tracked project

git status

Notes: This will show all files that have been changed, added, or deleted.

4. I want to commit all the changes I have made to my Git tracked project

git commit -m "changed fonts and colors"

Notes: -m "changed fonts and colors" is an optional comment.

5. I want to rollback to a previous commit and create a new branch called "dev" from that position

git checkout r39j145
git checkout -b dev

Notes: r39j145 is the first 7 characters of the commit number. -b stands for branch.

6. I want to see the history of all of my commits for my project

git log

7. I want to use GitHub from my terminal
Set up GitHub from to be used from the terminal here.

8. I've created a project on GitHub, now I would like to clone it and start editing the project

git clone git@github.com:MalcolmMcFly/DineMob-Web.git

Notes: git@github.com:MalcolmMcFly/DineMob-Web.git is the SSH path of a project on GitHub.

9. I've made some changes and committed them to the team GitHub project, I'd like to push them so my team can see them

git push origin dev

Notes: dev is the branch name.

10. My team made some updates to the GitHub project, I'd like to pull them into my local project

git pull origin dev

Notes: dev is the branch name.

11. A team member and I have changed the same line in our GitHub project causing a merge conflict when I try to git pull

git pull origin dev

CONFLICT (content): merge conflict in ...

git mergetool

Notes: Here mergetool which launch and let you choose which version of the file is correct. The mergetool will created a .orig appended copy of the file conflict file for you view how it looked originally before the merge; you may delete this file. Always perform a git commit after resolving merge conflicts.