The idea of this post is to list down most of the problems that you had with Git or will have in the future as well as some common questions about Git.
Of course, we will discuss about the best possible solution to solve the problem and answer the questions.
Here we go!
Problems & Questions
1. What is a branch in Git?
To quote from Atlassian, “A branch represents an independent line of development. Branches serve as an abstraction for the edit/stage/commit process.“
In simpler terms, a branch in Git is like a separate copy of your source code but not physically. Any work in a branch will not affect the other branches. When you git clone a Git repository, by default you will be in the master branch.
2. What are the reasons to use a different branch other than master?
When you are working on a project as a team, chances are high that each developer will have their own tasks or features to develop.
During the development, you will find yourself having to commit changes part by part. Imagine if you work on the master branch and somehow you commit broken codes, your original source code will then be broken and others won’t be able to pull from it to get a ‘fresh‘ source code.
Beside that, when working on multiple features, it is very common to have merge conflicts. If each developer creates a different branch, you will have a better time merging the branches into one branch before merging it into the master.
Another reason, sometimes people setup a CI/CD in which every time you do a push to the master branch, it will automatically be deployed. So you definitely do no want to work on the master branch as you WILL break the production app.
3. How do I create a branch, delete a branch or merge branches?
To create a branch, first you do
git clone <your git repo url>.
By default you are in the master branch when you do a fresh
git clone. To create a new branch, simply run
git checkout -b <branch name>. This will create a branch with name you specify and switch to that branch.
To delete a branch, run
git branch -d <branch name>.
To merge branches, first make sure you have more than one branch, OBVIOUSLY. Then, switch to the branch that you want to keep, usually developers will switch to the master branch since we want to bring the features from the other branch into our master branch. To put things easier way to understand.
// checkout into our master branch, you can checkout into other branch if //let say you want to merge two branches that are not master git checkout master //now you are in the master branch, we can bring a branch into our master //to merge through git merge <branch-name>
4. What is the difference between local branch and remote branch?
Local branch is your repo branch that is on your local machine.
Remote branch is a branch on a remote location or your repository.
Creating a new local branch does not mean you will have the same remote branch. You will have to push your local branch to your repository to have the same remote branch.
5. How do I switch to an existing remote branch if I haven’t created the local branch?
git clone <your repo URL>.
git checkout -b <branch_name> remotes/origin/<branchName>
You can run
git branch -a to find the remote branches.
6. What is the difference between git pull and git fetch?
In simple terms, when you use
git pull you will pull all the latest commits from your remote branch and merge it with your local branch.
git fetch on the other hand is telling Git to pull all the latest commits from the remote branch but hold it without doing the merge. So you can check the latest commits and then
rebase it with your local branch
7.What is the difference between git merge and git rebase?
git merge will combine multiple sequences of commits into one unified history. Basically you merge two branches, either merging different local branches or merging local and remote banches. You fix any conflicts during the merging and record the merge as it is.
git rebase is taking two branches, pick one branch to be the base, pull the commits from the second branch into the base branch, while pushing the commits from the base branch to the top. This creates a sort of linear history commits.
8. When should I git pull and when should I git rebase?
The Golden Rule of
git rebase is to never use
git rebase on public branches.
However it really depends on the situation, I personally love to use
git rebase almost all the time.
9. I accidentally commited changes with confusing message, how do I fix this ?
This is a really a common thing, as thankfully, Git has ways to remove the message back.
One way is using
git reset. By using
git reset you can rollback to the previous commit before the current commit with the confusing message.
You can use
git reset --soft HEAD~1 to rollback and also keep the file changes.
Another way is by using
git rebase -i but this a bit tricky and we will discuss more about this is the next post.
10. Why do people clean up their git commit history?
Well, as a developer myself, sometimes I do commit changes with really weird messages. That is totally fine if you are the only developer that is working with the repository but what if there are other developers other than you ?
Yeah, it can be kinda embarrassing when your colleagues read your commit messages. Additionally, this can be confusing for them too.
When we commit changes, we always want to write commit message that reflects the changes that we are committing so it will be easier for us to track it back later on.
There are ways that we can do to clean our git commit history clean. Let’s discuss that on the next post.
To be continued…
Hopefully the above 10 answers will somehow be helpful to you. We will be tackling more problems and questions.
Stay tuned for more in Part 2.