Basic Git Command Line
You only need to know a handful of git commands to navigate your branches from the terminal in your IDE.
Which branch am I on?
git branch
Git branch shows you a list of names of all your local branches. The branch that is currently checked out is highlighted in some way, maybe a star precedes it, or it is a different colour.
git status
Will show you only the name of the branch you currently have checked out.
How to make a new branch
Imagine you want to fork a branch from the branch that you currently have checked out.
You will want to make sure your original branch is totally up-to-date before you branch from it like so
git pull
Now that your local branch is up-to-date you are ready to fork from it
git branch my-new-branch
This would make branch called my-new-branch forked from your original branch
If you want to start working on this branch, you now need to checkout this branch...
git checkout my-new-branch
Committing your updates
Once you have edited your branch it is time to commit your changes to your local branch. First take a look at the changes you have made by listing the differences
git diff
This will show you all the changes you made, and the names of the files they are in. Decide which changes you want to commit and note the names of the files they are in. You need to add the names of these files into the staging area ...
git add src/components/file-name
You need to put the full pathname of the file.
Once you have added the required files to the staging area it is time to commit.
git commit -m "my commit comment"
this will make a commit named after whatever you put in the inverted commas eg "my commit comment", which will include all the files that you added to the staging area. If you were curious the -m stands for 'message', it means you are adding a commit message.
Your changes are now committed to your local branch. To push these changes to the remote branch you need to push like this;
git push origin name-of-branch
Switching branches
If you are working on one branch and want to switch to another one you must not have any unstaged changes on your current branch. This is because git would lose track of your current changes if you were able to do this. But what if you are not ready to stage your changes? You can create a stash where all these changes are held for you ready to unstash when you return.
git stash
...makes a stash of all your unstaged changes, you are now free to checkout a different branch
When you return to your original branch you can pick up your stash again like this
git apply stash
Stash is the quickest way to just revert your existing branch to allow you to jump across to another one. If you know you don't want the changes in the stash then use
git stash drop
to get rid of that stash.
More than you'll ever need to know about stashing...
git stash show
will show all your stashes before you do anything drastic like git stash clear
which would delete all your stashes.
However if for some reason you prefer not to stash, you could use
git restore my-file-name
where my-file-name is the full path of your file name. This will restore that file back to how it was when you first checked out the branch.
Made changes on the wrong branch?
Don't panic....and don't commit. It happens to the best of us, you forget you are on the dev branch and get stuck in to making some changes, but you can easily recover this situation.
- do a git pull to get dev from origin
- make a new branch and check it out
- now you can see your changes as uncommitted changes in the diff
- if you did git add before you made the new branch you will need
git diff --cached
to see the staged changes - commit these changes and push them - you're done.
Removing a file
Once git is tracking a file for you , it is not enough to simply delete the file from the directory, you have to tell git to stop tracking it as well. And so, similar to how you added the file into tracking at the beginning with git add, you now git remove it, like this...
git rm my-file-name
git commit -m "removing a file"
These git commands will be more than enough for everyday developing.