November 27, 2019
November 27, 2019
Collaborative software development has never been easier. Thanks to Git, you can easily monitor the modifications and contributions made to your projects. Stick with this Git tutorial and learn the software’s workflow, common commands, and other basic concepts.
Git is a Version Control System (VCS), developed by Linus Torvalds (creator of Linux) back in 2005. It can be used to track changes in any files, but it is commonly used to develop software.
So, what are the advantages of Git? Let’s take a look:
If you work alone, you can save the whole database in your local repository. However, when you work with other developers, the project should be stored and shared in a remote repository (or master repository), which is hosted on a platform like Github.
Since Git is a distributed VCS, users don’t have to access the master repository to create modifications. They can clone (copy) the master repository to their local machine, then modify the files without connecting to the network.
To understand the concept better, we will show you how to use Git in the next section of the tutorial!
Every local repository consists of three main sections: working directory, staging area, and git repository.
The working directory holds the actual files that you’ll modify. After that, changes will be temporarily saved (indexed) in the staging area.
In the second area, you still can make further changes to your files. When you’re ready, choose the changes that you want to save (commit) permanently.
Your git repository will store the commits. You can also push the commits to the master repository so other developers can check out your modifications.
Keep in mind that Git needs a username to be associated with the commits. As such, you should create an identity through this command:
git config --global user.name "name"
git config --global user.email "email address"
Thanks to this little detail, your teammates will know who commit the changes.
In this part of the tutorial, you can get a git repository through two methods — turn a local directory (folder) into a git repository or clone an existing repository.
For the first method, run the pwd command to know where you currently are. The command will print the path to a local folder.
If it’s not pointing to the folder that contains your project, run the following command to change it:
cd <path of directory>
After you’re in the right directory, run the git init command, so Git can track the changes made to the files in that directory:
git init will create a .git subdirectory within the local directory. This subdirectory is the one responsible for storing the snapshot of your changes.
Now, if you work on a collaborative project, you can clone the remote repository to your local machine and modify the files from there:
git clone <address of remote repository>
Let’s say you made some changes to a file named test.html. Before committing those changes, you need to put the file to the staged area by running the following command:
git add test.html
In this stage, you’re still allowed to make some alterations if you need to. Next, run this command to see whether all your changes are staged:
Then, commit the file:
git commit –m “Message to go with the commit”
Afterward, type the git status command again. If it says that there’s nothing to commit, it means that all the modifications are saved.
If there are some mistakes in your commit message, don’t worry! You can use this command to change the latest commit message:
git commit --amend -m “updated commit message”
After you save and exit, the message will be updated.
You can study the commit history by running the git log command. It will return a list of commits made in that repository along with the author, modification date, commit ID (hash), and message of the commit.
To retrieve the commits made by a single user, you can use:
git log --author =Smith
When you’re developing a software product, there must be a time where you commit changes and want to release it as a software update. Now, it is recommended to tag that commit so you can easily identify the release point in the future.
Here’s the command to do so:
git tag -a<version-number>
By default, git tag will tag the latest commit. If you want to tag an old commit, insert the commit ID (hash) after the version number
git tag -a<version-number> <commit unique ID>
You can obtain the commit ID from the results of the git log command. However, you don’t need to copy-paste the whole hash — the first eight to ten characters are enough.
In this section of the tutorial, you will learn how to work with git branches.
If you want to fix bugs or add a new feature, you might want to do it on branches, so the main line of development (master branch) won’t be affected when you develop that new feature.
Once the project in the branch is proven to be working, the branch will be merged with the master branch. By doing so, the master branch will have those changes, as well. We’ll talk more about merging in the next section.
To see the list of branches on your local repository, use this command:
If you want to create a new branch, just add the branch’s name:
git branch <branch name>
Now, although you’ve successfully created a new branch, you’re still in the master branch. To start modifying the new branch, you need to switch to it:
git checkout <branch name>
Don’t forget to add and commit the changes.
If you have old branches that had been merged and are not in development anymore, it’s a common practice to remove it:
git branch -d <branch name>
However, the command above will prevent you from deleting unmerged branches. Git is warning you because if it’s not merged, the master branch won’t have the changes you committed in the branch.
Force delete unmerged branches by running this command:
git branch -D <branch name>
Merging is a way to integrate changes from another branch. So if you’ve finished creating a new feature in a branch, you need to merge this branch with the master branch so the latter can receive the changes from the former.
First thing first, you need to move to the master branch by running the git checkout command. After that, execute the git merge command:
git merge <branch name>
Once you’ve committed the changes and want to share it with your teammates, it’s time to push your commits to the remote repository.
However, before pushing the commits, you have to make sure that the command line is pointing to the right remote repository address. Check it through this command:
git remote -v
If it’s not pointing to the correct address, you need to change it:
git remote set-url origin <remote repository address>
Now you’re ready to push the commits to the remote repository:
git push origin master
That’s it! You’ll see the commits from your local repository in the remote repository.
Remote branches are ones in your remote repositories. If you’re working on a collaborative project, you can say that these remote branches are the work of your colleagues.
Let’s say you cloned a remote repository and are working on a local branch. During this process, your teammates pushed their branches and made new changes to the remote repository.
Now, you want to update your working directory, so it will have the recent changes done by your teammates. To do so, you can use git pull or git fetch. However, there’s a difference between the two.
git fetch will download all remote branches and won’t affect anything in your local repository. It is a safe way to update your working directory.
On the other hand, git pull downloads the content of the remote repository and merges it with the current branch you’re working on. As a result, some conflicts might occur.
Aside from everything that you’ve learned in this Git tutorial so far, there are more concepts to discover.
Aside from merging, you can try rebasing to incorporate changes from a branch to the master branch. However, they’re not the same.
While merging would save all the commits from the branch in the form of a new commit, rebasing would move the commits to the top of the master branch.
Each option has its own advantages: merging won’t affect the log history, and rebasing will make the log history more linear.
However, using too much merge can make your log history unreadable as it will create plenty of new commits. Also, try not to change commits in a rebased branch to avoid conflicting code.
To improve productivity, you can automate your workflow through git hooks. They are custom scripts that will enforce a specific rule before or after an event.
You can use it on events like commit, merge, rebase, and push. Here are some of the examples:
Git allows you to undo commits and changes through one of these three commands:
Git helps you manage your software development projects. Whether you work alone or in a team, this software got you covered! You can easily create new projects, copy existing projects, make new features with branches, and share those projects with your friends.
We hope that this tutorial is helpful for those who want to get started with Git. If you need to learn other useful Git commands, check out our basic Git commands article.
May 09 2017
Hy Domantas, how i can use git in my hosting for manage my website ? i use the premium hosting in hostinger.co.id. Thanks before.
Replied on June 01 2017
Hey Muhamad, Yes, GIT is fully supported on our premium and business Hosting plans.