Get Good With Git
In this article, I will share with you some of Git’s commands that I often use and my experience applying Git Flow in one of my projects.
More Git Commands
Here are my most used git command other than the basics. To view all the commands, run: git --help
or git <command> --help
.
git branch
git branch
is a command to create a new branch or manage created branch.
Examples:
git branch <branch_name>
to create a new branch with<branch_name>
as the name.git branch -d <branch_name>
to delete a branch.git branch -m <old_branch> <new_branch>
to rename a branch
git checkout
git checkout
is a command to change branches.
Examples:
git checkout <branch_name>
to switch to<branch_name>
branch.git checkout -b <branch_name>
to create a new branch with the name<branch_name>
and switch to the new branch.
git remote
git remote
is a command to track a repository. Commonly used to connect a repository in GitHub or Gitlab.
Examples:
git remote add origin <URL>
to add a remote named origin for the repository at<URL>
.git remote remove <name>
to remove a remote named<name>
.
git clone
git clone
is a command to clone or download an online repository to your local computer.
Examples:
- git clone <url> to clone the remote repository to a directory named
- git clone <url> . to clone the remote repository to current directory.
git push
git push
is a command to push your commits to your remote online repository.
Examples:
git push origin <branch>
to push<branch>
toorigin
remote.git push -u origin <branch>
is similar with previous command, but will also set the upstream asorigin/<branch>
.git push
to push the current branch to its upstream. It will output an error if the upstream has not been set.
git pull
git pull
is a command to pull changes from the remote repository.
Examples:
git pull origin <branch>
to pull changes from remote branch<branch>
fromorigin
remote to your current branch and merge it.git pull
is similar with the previous command but will pull changes from the upstream that has been set. It will output an error if the upstream has not been set.
git merge
git merge
is a command to combine 2 branches into a single branch. It combines both changes into one.
Examples:
git merge <branch>
to merge<branch>
with the current branch.
git rebase
git rebase
is one of the most powerful commands you can do in git. It can move commits, remove them, merge them, and more.
Examples:
git rebase <branch>
to move current branch’s commits that is not in<branch>
history to<branch>
. For
If currently at "topic" branch: A---B---C topic <- HEAD
/
D---E---F---G masterIf you run command: git rebase master
The result would be: A'--B'--C' topic <- HEAD
/
D---E---F---G master
git rebase -i <commit>
to pick, squash, or revert commits at once. the-i
stands for Interactive.
git revert
git revert
is a command to revert or undo a commit. It only remove the specific commit by it’s reference or hash.
Example:
git revert <commit>
to remove<commit>
. For illustration:
A---B---C---D master <-HEADIf you run command: git revert B or git revert HEAD~2
The result would be:A---C'--D' master <-HEAD
git stash
git stash
is a command to temporarily store the changes on the current state. It will store modified files, but not untracked files.
git stash
to store the changes.git stash list
to view stashed changes.git stash pop
to get the last stashed changes and apply it to the current branch.git stash drop
to delete the last stash.
To learn more about these commands and even more, I recommend you to try some exercise in https://learngitbranching.js.org/.
Git Flow
When you are maintaining a repository or creating something alone, most of the time you only need 1 branch, which is master or main. But if you start collaborating with other people and everyone create commits on a single branch, it will get messy. That is why in a project maintained by more than 1 person, you need Git Flow. In the Git Flow that PPL 2021 implements, there are a few types of branch:
master
: the main branch that is stable and deployed to the production environment.staging
: the main branch where every developer will merge to before it is merged to master and deployed to production environment.PBI-n
: the branch that developers create and implement each Product Backlog Item, wheren
is the PBI number.hotfix
: the branch to quickly fix master branch when a bug is found in the production environment.coldfix
: the branch to fix or rollback changes in thestaging
branch if the product owner cancels a Product Backlog Item.
My Experience
In my software development team we tried to apply Git Flow. Every developer create their own PBI branch and develop there. After the PBI is finished, it will be requested to merge with staging and reviewed by other team members. After But in reality, we finalized our high-fidelity mockup last saturday and we started to code effectively only for 2 days, not enough for any of our Product Backlog Item to finish. I will update my experience later when we have merged our PBI and deployed the application.
Thank you for reading and I hope you find this interesting.
Reference
- Panduan Git PPL 2021.pdf
- Panduan Git Flow PPL 2021.pdf