Working with Git!
Ever wonder how a programmer team combines their works or their source codes into one, in a project? Or have you ever experienced when your current code failed and you want to change your code into your previous code that was made yesterday but you do not have the backups? Now, do not worry! Do you know about Version Control System (VCS)? One of the tools to implement VCS is Git. This thing will help you to solve the problems above!
Version Control System (VCS)
First of all, do you know about Version Control System or VCS? Basically, VCS is one of the software tools that help programmers to control their source codes, for example,
record changes of your codes or files, manage your code changes, allow to retrieve the old version of your code, and many more!
However, what are the tools to implement VCS? You can use git, for example. Let’s discuss further!
Git
So, what is Git? From https://git-scm.com/, it was described, “Git is a free and open-source distributed version control system designed to handle everything from small to very large projects with speed and efficiency.”. From that statement, Git is an open-source VCS tool to track your source code changes throughout software development.
Git uses the Distributed Version Control Systems (DVCSs). Here, every programmer has their own repositories. They can copy (or cloning) other repositories via server. The programmers also change the remote repository so that they can work together simultaneously in the same project.
Now, how can we use git? We can use the git commands. Want to know more about it? Let’s dig into it!
Git Commands
git init
git init is used to initialize a new repository or directory in your device or computer.
# change directory to codebase
$ cd /file/path/to/code
# make directory a git repository
$ git init
Here, for example, in my project. Because I have already initialized my repository, if I type ‘git init’, it will reinitialize my repository.
git remote
This command is used to manage your remote repositories. it shows you the list of remote and also connects your local repository to one or many repositories.
# Add remote repository
$ git remote <command> <remote_name> <remote_URL>
# e.g.
$ git remote add origin git@account_name.git.beanstalkapp.com:/acccount_name/repository_name.git
# List named remote repositories
$ git remote -v
# remove remote repository
$ git remote remove <remote_name>
# change the URL of a remote repository
$ git remote set-url <remote_name> <new_url>
Here, for example, I use ‘git remote -v’ command. As you can see, I have multiple repositories in my project.
git add
This command is used to add your files into your git repository. This file is stored in the staging area before you actually commit to your repository.
$ git add <file or directory name>
# add all file changes
$ git add .
I implemented this command in my project to add all of my changes into the staging area.
git commit
This command stores your file changes from the staging area (git add) to your local repository. For every commit, it has a unique ID.
# Adding a commit with message
$ git commit -m "Commit message in quotes"
-m here is to add a message regarding the file changes so that you remember or understand what things that you have changed to the code.
For example here in my project, I put a message that explains these changes was to reduce code smells and refactor some codes.
git status
This command shows the status of your repository or shows the current working branch
$ git status
Here, in my project, I changed my code but it informs me that I have not added or committed yet.
git branch
This command allows you to add a new branch to your local repository. You also can delete your branch and it shows you what branch are you on.
# Create a new branch
$ git branch <branch_name>
# List all remote or local branches
$ git branch -a
# Delete a branch
$ git branch -d <branch_name>
I implemented git branch on my project. It determines what are the branches in my local repository.
git checkout
This command is used to switch branches, and you can work your codes in a different branch.
# Checkout an existing branch
$ git checkout <branch_name>
# Checkout and create a new branch with that name
$ git checkout -b <new_branch>
for example, I want to switch my branch. You can use this command.
git push
This command moves your local commit (git commit command) into the git or remote repository.
$ git push <remote_URL/remote_name> <branch>
# Push all local branches to remote repository
$ git push —all
Here, I push all of my commits into my branch.
git pull
It retrieves the file changes from another remote repository into your branch or your local
$ git pull <branch_name> <remote_URL/remote_name>
Here, I tried to pull the file changes from branch staging into my local branch.
git merge
This command is used to merge your file changes with another branch.
# Merge changes into current branch
$ git merge <branch_name>
git clone
This command copies the existed git or remote repository into your local.
$ git clone <remote_URL>
git stash
If you don’t want to commit your changes, you can use git stash. It will save the changes and gives a clean working directory.
# Store current work with untracked files
$ git stash -u# Bring stashed work back to the working directory
$ git stash pop
git log
It shows you the history of your commits.
# Show entire git log
$ git log# Show git log with date pameters
$ git log --<after/before/since/until>=<date># Show git log based on commit author
$ git log --<author>="Author Name"
Here, it shows you the entire list of your commits.
git rm
This command is used to delete files from directories in commit section (staging area).
# To remove a file from the working index (cached):
$ git rm --cached <file name># To delete a file (force):
$ git rm -f <file name># To remove an entire directory from the working index (cached):
$ git rm -r --cached <directory name># To delete an entire directory (force):
$ git rm -r -f <file name>
git rebase
This command is used to combine your works into a new branch.
# To rebase the current branch to another branch
$ git rebase <branch_name>
# undo rebase
$ git rebase --abort
This command will move all of your commits into the new branch.
Implementing Git in My Group Project!
How do I use git in my software engineering group project? I will tell you now!
In my project, we use Gitlab as the platform, hosted by the faculty. to because we continue or senior’s work, we have to clone the existed git repository into our locals by using git clone <link repo> command. Each of the group members has their own tasks, we divide it into some user stories. We create a new branch for every story by using git checkout -b <branch name> command. I do my work in my local branch. I use git add, git commit -m “<message>”, and git push origin <branch name> commands to store my changes into my local branch. After the tasks are finished, we can start to merge our works into the staging branch. We can use git merge command, but I prefer to use ‘merge request’ feature in Gitlab website.
We combine all of our works in the staging branch. The staging branch is also used to fix any bugs that appear in our works. When the staging is ready, we can present our works into the client. However, if the client tells us to make some changes from the staging branch, we can fix it into a new branch called, coldfix, then merge it into the staging branch again. And if it is proved by the client, we can push it to the master branch. If suddenly the client gives a revision again, but we already pushed it into the master branch, we can fix it into the hotfix branch, then merge it into the master branch again. And so on.
That is all for me! I hope this article helps you to understand more about version control system, especially git! Thank you for reading until the end! 🥰
source:
https://www.geeksforgeeks.org/version-control-systems/#:~:text=Version control systems are a,(snapshots)%20of%20the%20project.