Table of Contents
Introduction
Git is an incredibly powerful software version control system. The downside is that the commands can be a bit cumbersome and hard to remember at times. In this blog I list the commands I either use everyday or have to look up frequently.
More blog posts:
Setting Vim as the Default Editor
By default git will use gedit or nano as the default editor when writing commit messages. I prefer vim:
$ git config –global core.editor “vim”
Cloning a Repository
Cloning a repository makes a copy of the remote server and downloads it to your local computer.
$ git clone https://path-to-remote-server LOCAL_REPO_PATH
Adding and Committing Files
To track changes to a file it needs to be moved to the path of the repository and then added:
$ mv FILENAME LOCAL_REPO_PATH
$ git add FILENAME
The file is “staged” for commit, but changes aren’t being tracked yet. Commit the file to start tracking changes:
$ git commit FILENAME
Pulling Changes
“Pulling” changes updates your local repository with the most recent changes stored on the remote server:
$ git pull
It is good practice to “git pull” before pushing to a remote repository.
Pushing to a Remote Repository
Often you’ll be using a remote repository or server (such as GitHub) to act as a central hub connecting multiple users on a software development team, or as a backup for your own development. In either case, you’ll need to push changes from your local repository to the remote repository:
$ git push -u origin master
Branching
Branching is a way to make a copy of the source code which can be edited and tracked separately from the master branch. Branching allows multiple users to work on the same source files, make modifications to them, and then intelligently combine their changes later through merging.
To create a local branch:
$ git checkout -b BRANCH_NAME
Merging
Merging intelligently integrates revisions of source code from one branch to another. In this example, the source code from BRANCH_NAME is merged into the master branch:
$ git checkout BRANCH_NAME
$ git pull
$ git checkout master
$ git pull
$ git merge BRANCH_NAME
Pushing to a Remote Branch
At this point you’ve made some local changes through a commit, a merge, or some combination. To update the remote server, you will need to push them. After you’ve created a branch for the first time, you will need to define the remote server name and branch name when you push:
$ git push -u SERVER_NAME BRANCH_NAME
Typically, this is:
$ git push -u origin master
where origin is the remote server name and master is a common branch used in git.
After you have pushed to a remote branch once can use a simpler command:
$ git push
Deleting Branches
Now it’s time to delete a branch. Maybe you’ve merged changes off a temporary branch, or you just have too many and it’s time to clean up. To delete a local branch:
$ git branch -D BRANCH_NAME
To delete a remote branch:
$ git push origin :BRANCH_NAME
If you have been working on the branch software-patch, then you can delete it by:
$ git branch -D software-patch
$ git push origin :software-patch
Pruning Remote Branches
You will need to “prune” remote branches from time to time. For example, someone else may have deleted a remote branch on the server and you want to update your system with that change. This requires a prune:
$ git remote prune origin
Tagging
Tags are similar to branches, but they are often used to maintain previous states or software releases. For example, tags might be created for software releases v1.0, v1.1, v1.2, etc.
To create a tag locally:
$ git tag -a TAG_NAME -m “commit message”
The TAG_NAME can be tied to a specific software release, say v1.0 for example:
$ git tag -a v1.0 -m “Creating the v1.0 tag”
The tag needs to be pushed to the remote server:
$ git push origin TAG_NAME
which in this case is:
$ git push origin v1.0
Conclusion
Git is an incredibly powerful software versioning system but it can be tricky to use at times. This blog post covered the most popular and useful commands when developing with git.
More blog posts: