main banner

Development

Learn How to Git & Flow – Part 2

You can read part one of this article here.

A Few Good Commands

Using the Command Line is the best way to enable Git’s full potential. However, it’s probably also the scariest aspect of Git because of the insurmountable amount of commands Git can use. This can be very overwhelming for a lot of people, and with good reason. But fear not! Because out of all those commands, there are only a few you must learn to be an effective Git user, and whenever you need some of the more obscure commands you can always take a look at the documentation here: https://git-scm.com/doc.

Basic Commands

As we discussed before, Git already has a defined workflow in which it tracks the files of our project and it essentially revolves around Commits. So, in order to make this flow work, these are the basic commands you must master:

  • git add . – this command is used to move all of our untracked files from our Working Area to our Index so that Git knows that there are new files that can be committed. We make use of the “.” to add all files.

  • git commit –m “Descriptive message” – this command is used to create a new Commit for all the files we added to our Index previously. It is a good practice to add a descriptive comment to our commits so that we can track the changes made in each Commit.

  • git push – once we decide we are ready to add all of our new commits to our project, we must use this command to “push” all of our changes (new Commits) from our Index to our Repository.

  • git pull – this command is the direct opposite from ‘git push’ and what it does is to “pull” any new Commits from our Repository that we don’t currently have in our Index and Working Area.

That’s it! That’s all the commands you need to enable the basic workflow of Git. However, these are not the only essential commands you must know, there are a few others that will really empower you to use Git and make your experience much easier. For example, how do I know if there are untracked files in my Working Area? Or, how do I manage multiple branches? Well, there are a few other commands you surely want to get acquainted with:

  • git status – allows you to see the state of your Working Area against the Index to show you if there are any untracked files and also compares the Index to the Repository to see if there are any pending Commits to be pushed.

  • git checkout -b <nameOfBranch> - want to have more branches? No problem! Just run this bad boy and it will create a new branch based on your current one.

  • git checkout <nameOfBranch> - well, of course, you want to manage multiple branches! This command allows you to change your current branch and “check out” other branches in your Repository. 

  • git branch –dused to delete a branch, as simple as that. If you know what you are doing and want to force-delete a branch just use –D instead.

  • git stash – this is a helpful command when you want to check out a different branch and you have untracked files you are still not ready to commit. Since Git won’t let you change your current branch until you commit these changes, you can make use of the Stash to keep your changes safe, check out any other branch and come back. Simply use git stash pop to recover your last stashed changes.

  • git merge <nameOfBranch> - this command merges the given branch name to your current branch, thus combining any changes each may have. There is a similar command called rebase but it’s not very recommended to use it and we will see why in a second.

  • git log –graph –decorate –oneline taking a quick look at those more obscure commands, this is a particularly useful command to visualize your branch’s history in a neatly arranged graph. I really recommend trying this one out, even if it’s just for fun.

Don't miss the third and final part of this article which will be available next week.

Pablo C.

Bachelor of Science in Computer Engineering working as Front-End Developer. Amateur UX designer, free time Android Developer, passionate RPGs gamer and mayor Star Wars fan.

Articles