Home Useful GIT Commands
Post
Cancel

Useful GIT Commands

Finally, my return to this blog after several months working in a Japanese web development company. Other than showing off my projects I haven’t had any other plans for this blog, but as I gain more experience as a developer I have started to realize how much I use online resources to get things done - a wide range of blogs and how-to guides that go far beyond Stack Overflow pages. Documenting the kind of information I use to build stuff is more than just good practice I think, it’s potentially a valuable resource for others, and at least an essential personal record.

Here I’ll list my most common Git commands that I have been using on a daily basis since starting a new career as a web developer.

GIT an introduction

I won’t go into the history of GIT here, and I won’t go into the how to install it. Instead I’ll just describe GIT from a basic user’s perspective. Git needs to be installed, of course, and I’m writing this as if you would be using git from a command line interface. It’s also possible though, to do some GIT stuff on a remote repository service such as Github, or through a simple interface in a code editor such as VisualStudio Code.

So, what is GIT? GIT is a version control tool that allows developers to save changes to documents, and then if necessary go back to a different stage in the history of a project’s development. In large projects that are being developed as a team, GIT becomes an essential developemnt tool, but even as a solo developer it would normally be used by a professional developer in order to manage the development work. Another aspect of GIT is the ability to create branches which can then be merged back into a main branch (kind of like a tree trunk), or alternatively a branch may fork off into something else, i.e. a different project with different features and aims. (That was my one paragraph description of GIT, round of applause, thank you very much.) Now I’ll list the GIT commands that I’m using all the time.


git init

git init is actually a command I use more rarely in comparison to the others here because as a developer I’m usually brought into projects that have already been set up. However I do use this one sometimes when I’m coding something from scratch with say just an HTML file, and a javascript file in a project.

git init is going to initiate the GIT version control system in a project directory. Once initiated all files in the directory will be “tracked” so to speak.

1
git init

git add ., git commit

git add and git commit are kind of a pair so it makes sense to put them together. The git add . command will, according to everything I’ve read such as this, add files to the “staging area”. In other words this command tells GIT to put all files that have been saved and changed since the last commit, to go to the staging area and then prepare for the git commit command which will mark a stage in the history of commits.

1
git add .
1
git commit -m commit_name

git pull, git push

git push and git pull are another pair. Basically, the push command will upload commits somewhere, and the git pull will download commits somewhere. Typically git pull is used like this:

1
git pull origin master

This command will “pull” the commits from the master branch from the remote GIT repository named ‘origin’. A typical git push command might be something like this:

1
git push origin branch_name

Usually new features are not pushed directly to the main/master branch in order to avovid messy, conflicting code. So in the above example the newly created commits on a local dev environment would be pushed towards a separate branch on the remote repository. A “pull request” would then be created which would later be reviewed and then the branch would be merged into the main branch.

Pulling and pushing code is basically the everday work of modern developers. From these two commands, a number of associated commands become necessary in order to effectively use GIT.


git checkout

The GIT checkout command is used to switch between different branches. I guess the GIT engineers could have called it git changebranch, or something - but they didn’t.

1
git checkout branch_name

git checkout -b

In order to create a branch git branch -b is used. An interesting thing to remember here is that when using this command a new branch is created and based on the branch from which it is made. That may sound complicated, but is an important point when dealing with merging and rebasing branches.

1
git checkout -b new_branch_name

git branch -D

Deleting a branch is done with git branch -D branch_name. At this point, and with all the preceeding git commands memorized you can navigate, create, and delete git branches - very useful.

1
git checkout -b new_branch_name

git branch

git branch will list all the branches in the current directory.

1
git branch

So, this is my first list of useful git commands. These basically got me through my first 6 months of coding using GIT as my version control system. However, I soon realized I needed to learn a few more GIT commands to help me when I started daily work with a larger team of developers in a web development team. I’ll put these in a later post.

This post is licensed under CC BY 4.0 by the author.