Starting with Git on Linux (Part 4)

Starting with Git on Linux (Part 4) is part of the series Starting with Git on Linux. See all links of these series at the end of the article.

At Starting with Git on Linux (Part 3) we talked about initializing a repository, starting version controlling and proposing changes.

In this part, we will talk about pushing changes and branching.

In the part 3, we learned how to propose changes with the command git add and commit the change we made with git commit command. But the changes we made are now in our local working copy and we want to send these changes to our remote repository.

For example, we have created a GitHub repository at https://github.com/example/example.git To push our local repo to the GitHub server we’ll need to add a remote repository. The following command we will use takes a remote name and a repository URL, which in our case is https://github.com/example/example.git.

The command we will run in Terminal to add a remote repository is:
$ git remote add origin https://github.com/example/example.git

To sent the changes to the remote repository, run the following command:
$ git push origin master

The name of our remote is origin and the default local branch name is master. You can change the master to whatever branch you want. I explain branches after a while.

In order to remember the parameters we will add -u to the command above. -u tell Git to remember the parameters, so the next time, we can simply run git push and Git will know what to do.

$ git push -u origin master

One of the best features of Git is branching. If you are working in a big project branching is very useful. If you want to demonstrate your old work, you could switch over to your old branch. If your idea is approved, you can then merge the new branch with the old one to make the changes.

So, branches are used to develop features isolated from each other. The master branch is the “default” branch when you create a repository. I suggest to you guys, to use other branches for development and merge them back to the master branch upon completion.

To check the list of branches and the current branch that you are working on, see this command:
$ git branch

When creating branches, you need to name them properly, just like functions. To create a branch, run the command below:
$ git branch [branch_name]

The command above creates a new branch based on the last commit but remains on the same branch. If you want to switch to a different branch, type in Terminal:
$ git checkout [branch_name]

When you create a branch, I suggest to create it and switch to it immediately with the following code:
$ git checkout -b [branch_name]

In this part, I explained pushing changes and branching. These series are not over. In the following days, I will continue to write about Git in Linux.

This article is part of the series Starting with Git on Linux.