Feb 16, 2017 3:00 AM

7 simple commands for Git survival

You don’t have to get fancy to get most of your work done with Git; start with these essential commands


More companies are using Git, and not only for developing business and consumer apps. Science and government organizations are also increasingly working with the technology, for good reason.

Git allows for increased collaboration, faster deployments, and more code-building confidence—three all-important goals as more teams start adopting a devops approach. To help meet those goals, many Git platforms now include integrated tools for code testing and team management.

Even as Git has become ubiquitous, there is still some fearfulness surrounding its adoption. GitLab recently conducted a survey and found that 40 percent of dev teams are concerned about the learning curve associated with Git. Naturally, at GitLab, I work with Git every day, and I understand the challenges Git can pose for new users.

Fortunately, there are plenty of resources available to help new users get over their Git anxiety. Many companies, including ours, have free resources available for devs who want to learn Git. In addition to company-led resources, Git is supported by a number of helpful communities that share solutions to common problems and are ready to answer your questions. 

Keep Git simple 

To grok Git, it’s a good idea to get to know it from the command line. But even experienced Git users prefer Git clients that make Git accessible through a simple interface. Git Tower and Tortoise Git are excellent examples of such clients.

The good news is, most developers can do the majority of their Git-related work by learning only a handful of basic commands. The seven commands I walk through below are all you need to sync with a repository, create a new branch, make adds and commits, and push your changes back to the remote. Git doesn’t have to get complicated.

Switch to the master branch with git checkout master

A git checkout allows you to move between branches and potentially restore tree files. The command git checkout master switches you to the master branch, which is always the best place to start before making changes to your repo. 

Get the latest updates with git pull origin master

Once you’re on the master branch, you can use the git pull origin master command to ensure that your branch is up-to-date with the repository. This is typically done to merge upstream changes. A git pull is actually a combination of git fetch, which grabs all the latest information, and git merge, which merges the two histories together. Essentially, git pull origin master allows you to do two commands at once. It’s a great time-saver!

It’s always a good idea to run git pull origin master before starting work on a repository. After all, you want to be sure your repository is up to date with the remote repo where you collaborate.

Extend your branch with git checkout -b branchname

This command is used to create a new branch called “branchname” and move to it.

Branching out is essential to working with Git. Got an idea for a new feature? Enter git checkout -b new-feature to create a new branch called “new-feature” and open it. The new branch allows you to work in parallel with your colleagues, keeping your code separate from theirs during the time you’re working on that branch.

When you’re ready to share your work, you can push your branch to a remote repo or merge it back into the main branch (usually master). Those commands are coming right up ...

When you need a gut check run git status

Let Git tell you the current status of your repository. For example, Git will indicate which files have been changed or added as a result of the changes you’ve made on your branch. Then Git offers suggestions on what to do, offering commands on how to stage or commit those files.

Prepare for the next step with git add

The git add command appends a change in the working directory to the staging area. A change can be either a removal or an addition of a file or directory. This a preparatory step toward committing your changes. The actual actions are already “done,” but this command officially nominates those changes to be committed.

If you’re happy with all your changes, then you can add the —all option. Entering git add —all stages all changes in the working directory and subdirectories, including removals of directories.

Record your changes with git commit

The git commit command records changes you make to the local repository—not to be confused with git push (explained below), which updates a repository. You can also use git commit to delete files, though it’s a somewhat roundabout way of doing so.

Git is essentially a tree of commits, where a commit is a change (an addition, deletion, or update). Doing git commit will commit the changes that you have staged with git add. The commit is made on your local repository. It must be pushed to remotes (repositories not on your computer) to be shared.

Release your changes with git push

Run the git push command to push your changes to the repository. There are a variety of ways you can tweak this, as you can combine the push command with exceptions. On its own, git push makes changes to the repository and all of its associations.

The git push <remote> <branch> command will push the changes on <branch> from your local repository to <remote>, which is usually the repository on a server where you collaborate with your colleagues. By default, the first remote is called "origin." If you’ve made changes in your new-feature branch, you’d do git push origin new-feature to send the changes (commits) of new-feature to the place where you collaborate with your colleagues.

You really can do 100 percent of all of your Git-related work by knowing the above commands. Where developers often find themselves getting into trouble is when they try to overcomplicate matters with complex commands such as git rebase, which can lead to confusion about what they’re doing and whether it’s necessary.

When in doubt, run git status and read what Git tells you!

New Tech Forum provides a venue to explore and discuss emerging enterprise technology in unprecedented depth and breadth. The selection is subjective, based on our pick of the technologies we believe to be important and of greatest interest to InfoWorld readers. InfoWorld does not accept marketing collateral for publication and reserves the right to edit all contributed content. Send all inquiries to newtechforum@infoworld.com.