Git Guide for Good
In the large scale of software development, not just teamwork, we also need the right environment to work as a team. We can have something called Version Control System (VCS), which allows us to work dynamically in a team, and Git is one of the best VCS to use. Git is considered the best choice mainly because it has the functionality most developers need, and it’s free!
What can we do with Git?
In the term of software development, there are some steps and actions we can do with Git. But before that, we need to know ‘trees’ in Git. Trees in Git has 3 part and each of those tell us in which step is our job.
- Working Directory: The part where we code our project
- Index/Stage: To commit our changes
- HEAD: Refer to the last commit.
Here are 6 things you can do with Git:
Setup
As we mostly code in out local repository, we have to make a setup in our local repository. This step remarks the first step of working directory.
First, initialize git folder using Git init command.
git init
if you already have the project you want to continue in local repo, you can clone the repository by using this command :
git clone /path/to/repository
Add & Commit
The next step is to add files and/or changes to.
To add a specific filename we can use command
git add <file-name>
If we want to add all changes in the whole project, we can use :
git add .
if we want to add all files but one specific file, we can make exceptions by using this command
git add .
git reset <file-name>
Or maybe if we want to cancel the changes on a specific file, we can use :
git rm <file-name>
After add the files, commit to the changes in the git repository. This step will remark the end of stage part, and after files are successfully committed, we’ll move on to the HEAD part.
git commit -m “Meaningful commit message”
Push
The next is to push to our repository. This stage will put our commits of changes in the online git repository.
To push the commit, you can use this command :
git push origin <branch-name>
If you haven’t already set any remote server, you can add your online repository link by this command
git remote add origin <server>
Branch
As Git is designed for a big project with several programmers, if every programmer does different tasks and commits it all in the same place, there will be a lot of conflicts in file. Thank Git, for having a branch feature and letting us commit several jobs separately. This feature allows us to do project separately from other programmers. We also can use a specific branch to do a particular feature so that it will be easier to take a look for a progress on that feature. Here’s how to use branch :
To create a new branch and move to it, use this command :
git checkout -b <branch-name>
To move into an existing branch, use this command:
git checkout <branch-name>
If you want to remove a branch, use this command:
git branch -d <branch-name>
To see a list of existing branches, use this command:
git branch
Update & Merge
Sometimes in development, we have to update our file in the local repository, with the most updated one in the ‘main’/’master’ branch or we will merge a branch with other branch. Here’s how to use merge and update in Git.
To update the local repository to the latest commit in the current branch, run this command:
git pull
To update the local repository to the latest commit in the other branch, run this command:
git pull <remote> <branch>
To merge other branches to the current branch, run this command:
git merge <branch>
History
To view a list of existing commits, use this command :
git log
Stash & Revert
Sometimes when we want to switch to another branch, we have some uncommitted files in our local repository, to save those change, we can use stash by doing this command :
git stash
When we what to undo our change, we can revert the last commit with this command :
git revert HEAD
That’s a brief explanation of Git. I hope it helps you all who will to doing software development in the near time. Thank you!