Here comes Git: Common Problems and Questions Part 2. If you haven’t read Part 1, you can read it here Git: Common Problems and Questions
Questions & Problems
1. What does git status
shows and how to use it?
git status
is a git command that will shows you any changes that you made in your current branch. It will shows whether if you have modified, added or deleted a file. Always remember to run git status
before you do anything like git pull
, git rebase
etc etc.
2. What does git stash
do and how to use it?
git stash
is a git command that allow you to save any uncommitted changes. By running git stash
you will take all the current uncommitted changes, stash and you will return back to the clean working directory.
You can run git stash list
to view the list of your stashes and you are able to retrieve back that stash with git stash apply stash@{x}
with x
being the number that you want from the git stash list
.
3. What are the situations where I need to use git stash
?
Let’s say you are working on your branch, minding your own business and suddenly you need to run git pull
but you do not want to commit your current changes just yet. So you can git stash
and then git pull
. You can then run git stash apply
to retrieve back you changes. If there is any merge conflicts then you will have to solve it.
But its not just this situation, you may also use git stash
to change between branches and then apply your stash into the branch that you just checkout.
4. I have multiple commits that actually trying to achieve the same feature, is there a way to consolidate or combine it?
Sometimes when you are working on an isolated branch to produce a new feature for the project, you will end up having multiple commits which actually does the same thing.
For example, lets say you are working on a UI fix and you have the commits like so
a9u0864.. changed the hover color of the submit button b6u0123.. changed the background colour of the submit button r3u0470.. fixed typo for button text e1u0470.. changed the text of the button 1q7476e.. added border-radius to the submit button
So the above 5 commits are generally about changing the styling of the submit button and you feel like it should just be combine into one commit. But how to combine it ?
What you can do is by running git rebase -i
followed by rewording and squashing it.
When you run git rebase -i HEAD~<number of commits you want to squash together>
, for example since we have 5 commits we can do like so, git rebase -i HEAD~5
.
The editor will open up and press i
to enter insert mode
to modify the message of the commits. Change the top commit to reword
and the rest to squash
. Press escape
key to go out of insert mode
and then type :x
to save.
In some cases, the editor might ask you to type in the new commit message, you can use i
to enter insert mode and type in your message
Voila~
5. What is git rm
and when do I use it?
With git rm
you can remove files or directory from you branch.
Yes, you may also remove or delete files/directories manually or use rm
from the command line but you will have to use git add <removed file>
later.
6. What is git mv
and when do I use it?
With git mv
you can move files/directories from one location to another within the branch. git mv
also stages the move which mean we can straight run git commit
after running git mv
Like above, we may also move files/directories manually or use mv
from the command line but we would have to stage the changes later with git add
7. What is git cherry-pick
? When do we use it?
Git allows user to do cherry-picking which means that user can pick any commits from a branch and then apply it to another branch.
Previously, we talk about git merge
or git rebase
to bring commits from a branch you are working and apply it to the master branch or another branch. But problem with these two git commands is that it generally apply multiple commits and its hard to pick only the commit/commits you want.
With git cherry-pick
you can do so easily. For example:
//lets say you have commits from a branch called rasyue that you want to //apply to master //first checkout into the master branch git checkout master //then simply run git cherry-pick <commit hash> //you can view the commit hash from your branch with git log
8. What is git revert
and git reset
? When do I use them?
git revert
is a git command that allows you to rollback to the last commit. The commit that you reverted will be removed and a new commit will be created. For example,
// lets say you have 2 commits e1u0470 mistake commit 1q7476e first commit // we would want to revert our latest commit which is `mistake commit` git revert HEAD //this will result in 2mt0aby Revert "mistake commit" e1u0470 mistake commit 1q7476e first commit //a revert commit will be created but the mistake commit will not be //removed
git reset
is a git command that allows you to reset the current HEAD to the specified state. git reset
also accepts three different options namely git reset --soft
, git reset --mixed
this being the default, and git reset --hard
with each giving different result
Example on using these commands:
// so lets say you have 3 commits namely e1u0470 mistake commit 1q6966e second commit 1q7476e first commit // you can reset that third commit 'mistake commit' simply by running git reset <commit hash> //notice that we do not specify the mode i.e soft, reset or mixed // by default the mode is mixed if not specify
Now, lets talk about the three options or mode namely --soft
, --mixed
, and --hard
.
git reset --soft <commit hash>
will reset to the commit but leaves your changes.git reset --mixed <commit hash>
works the same way as --soft
but leave your changes in the working tree.git reset --hard <commit hash>
reset everything and does not leave anything.
9. Oh shoot! I accidentally ran git reset --hard
. How do I undo it?
One way to undo git reset --hard
is by running git reset --hard
again but this time you will need to find the commit hash of the commit that you reset.
Run git reflog
to find the commit you had accidentally reset.
Then, run git reset --hard <commit hash that you want>
.
Voila~
10. What is git notes
? When do I use them?
git notes
is a git command that allows you to add notes or longer message to a commit. When you run git commit -m "<msg>"
you would want to keep the commit message short and precise. You can use git notes
to add a longer description to the commit like so:
// run git log to get the commit hash git log // copy the commit hash of the commit you want to add a note git notes add -m "<your note>" <commit hash> // you can view the notes back with git show -s <commit hash>
11. What is .gitignore
?
.gitignore
is not a git command but it is actually a file in which you can specify any files/directories that you want git to ignore or not being track.
In every source code that we are pushing to a git repository, there are going to be some files that we do not need to push to the repo for example any libraries directories like node_modules
or vendors
just to name a couple.
By specifying these files/directories in the .gitignore
file, git won’t be tracking them and won’t be push to the repository
12. What is a pull request? How do I use it?
A pull request is a Github feature that allows a developer to request the owner of a repository to pull his/her changes in the local branch and include it into the repository.
In simpler words, let’s say you are working on an open source project and you finished up a feature on your local branch. You can then request the owner of the repository to pull your local changes and simply incorporate or merge it into the master.
However, the process is not that simple, usually, once you have finished a feature, you will have to do a Pull Request(PR) on Github and the peers will have to review it first before proceeding to merge it.
13. Is Pull Request (PR) the same thing as git request-pull
?
Although the name might be similar, unfortunately these two are a total different thing. A Pull Request (PR) is a unique feature of Github while a git request-pull
is a git command.
What git request-pull
does is generate a summary of pending changes and is use in the mailing service of git.
The End..
Okay, that is all folks. Hopefully this will be able to help you in some way or another. If you want to do more reading, I strongly recommend you read Git. It can be a bit confusing at first but you will get the hang of it.
Keep up the hustle!