15 Essential GitHub Commands for Beginners
GitHub can be confusing for beginners, especially if you do not have experience with version control software. As using GitHub is a vital skill for many IT professions, in this article we will highlight and explain the essential commands that you need to know.
This article assumes that you have already signed up for a GitHub account and have installed Git on your computer. It also assumes that you have basic familiarity with a command-line interface. Let's get started!
Git provides a very useful help system for those starting out. To get detailed help on any Git command, enter git help <command>. For example, git help clone provides you information about the clone command. To list all commands available, enter git help -a.
Git also provides guides, such as tutorials. To list all available guides, enter git help -g. To view a particular guide, enter git help <guide>.
You use the git config command to set your configuration settings. Usually, you only have to use this command when you are first starting out with Git. The most important settings are your user name and email, which Git uses to track your work, and you can set these with the following commands: git config –global user.name = "<your username>" and git config –global user.email = "<your email address>".
While your user name does not have to correspond with your GitHub username, you want to make sure that your email addresses match.
You use git init to initialize an entirely new project or one that exists only on your computer. It creates a Git repository (and its associated files), which is required before you can use most other Git commands.
Typically, you run this command from inside the root directory of your project, though you can also use git init <directory> to create a new repository within the directory that you specified.
Often GitHub users do not start their own projects but instead work on projects that have been already created by others. The first step in doing this is cloning the project's Internet repository, which is called a remote repository, onto your computer. To clone a repository, enter git clone <repository URL>. This command will initialize a repository on your computer (using git init), and then copy the files from the remote repository. It will also establish a connection to the repository URL called origin, which will make it easier for you when issuing other Git commands that operate on the remote repository.
You can also clone a new repository by first creating it on GitHub and then adding some initial files there, such as a readme.
After you create (or clone) a repository, you will likely make a number of modifications to the files within it, which can include adding, deleting and editing files. The first step in processing these changes is to use git add, which tells Git to add these changes to what is called the staging area. The staging area includes all updates that will be made during a particular commit, which is called a snapshot.
You can add a file or directory by placing this as an argument of the command, or you can add all changed files in a repository to the staging area by using git add -A.
You will often use git status in combination with git add, as it tells you which changes to the repository have been added to the staging area since the last time you committed changes to the repository as well as which changes have not.
As an added benefit, the command will also give you instructions on how to move any unstaged changes to the staging area.
Once you have a snapshot that you want to commit to the repository and have added these changes to the staging area, you are ready to have these changes updated in your local repository. You do this by using git commit. You must issue this command along with a message that describes the changes that you have made.
For example, git commit -m "added a graphic user interface". It is important to make this description as meaningful as possible, as it tells other GitHub users what exactly you have changed.
It is also important to note that using this command does not make any changes to the remote repository on the Internet. It only changes your local copy of the repository.
The next step in making changes to a project is when you push them to the remote repository using git push <remote URL> <branch>. The remote URL parameter is what you specified when you cloned the project, but you can also use origin, which was defined when you originally cloned the repository.
The branch parameter is the branch that you are updating. If you are updating the master branch of the repository, you use master. So, the following command updates the master branch of the default remote repository: git push origin master. If you push the changes to a branch other than the master, you or someone else at some point will have to merge these changes into the master branch using git merge.
You can ask the owner of a remote repository to merge your changes through making a pull request on GitHub, which should not be confused with issuing a git pull command on your local repository.
You can look at git pull as being the opposite of git push. You use it when you want to update your local repository with changes that have been made to the remote repository. It actually issues two separate Git commands: git fetch, which will download the remote repository, and git merge, which will merge the changes into your copy of the repository.
You can perform a variety of operations on the branches of a Git repository by using git branch, including listing existing branches as well as adding, deleting and renaming branches. Using the command without parameters will list all current local branches, with the current working branch highlighted and signified with an asterisk.
By entering git branch -a, you will see all branches, including those that exist only in the remote repository. If you want to add a branch, you can do this by entering git branch <branch>, but it should be noted that this will not make this new branch the currently selected branch.
You can switch to a specific branch of a repository by using the git checkout command along with the branch that you would like to use. For example, git checkout mybranch. The command also provides a convenient means for both creating a branch and switching to it, by using the following syntax: git checkout -b <branch>.
At times you may want to temporarily save changes that you have made to a project without committing them to the repository right away. Using git stash will store both staged and unstaged changes for later use while reverting these changes from your working copies. Then, when you are ready to restore these changes, you can either use git stash pop, which will restore the changes and remove them from the saved stash, or git stash apply, which will restore the changes while keeping them in the stash. The latter is useful if you want to apply the stash to multiple branches.
You can create multiple stashes as well, and manipulate individual ones by specifying a particular stash.
As stated earlier, when you run a git pull command, it itself runs a git fetch and a git merge command. But there are times in which you might want to download the latest remote repository (or a particular branch of it) but not actually merge this into your local copy.
It should be noted that fetching a particular branch will not switch you to that branch. You will still need to use the git checkout command.
If you are managing a GitHub project, at some point you will probably want to merge the changes reflected in a particular branch to the master branch of the project. You do this by first switching to the branch that you want the changes merged into (most likely the master branch) and then entering git merge <other branch>. Once you have merged this branch into the master branch, you can safely delete it using the branch -d <other branch> command as it is now included in the master branch.
When you clone a repository, Git establishes an origin connection to the remote repository, but there may be times when you need to work with multiple remote repositories. You can use the git remote command to do this. You establish a remote connection by entering git remote add <remote name> <remote URL>, and you can list currently established connections by entering the command with no parameters or with git remote -v. The latter will further list the URLs of the defined remotes.
In conclusion, GitHub is not so confusing once you have a thorough understanding of its essential commands and their purposes. As you explore GitHub more, use this as a guide to help you build your knowledge and leverage the platform more effectively.