Git Command Line Basics
It’s 2020, you have started to lean coding it’s great to use an GUI like gitKraken however in some roles you might not have the luxury of using a UI an have to perform git requests from the command line. Today we are going to cover the important commands you will need to learn.
git init Initialize or reinitialize and existing repository with a .git folder that tracks our project locally, or in other words it creates a git project ready for all those fantastic lines of code to be tracked.
git add stages our modified or new created files ready for us to commit them to our repository. To make a bit more sense, Git has three states. The first is if a file is modified or created. The second is if a file is staged, which means if you prepared the file to be committed. And lastly if a file is committed, which means that our changes have been stored to our local repository, ready to be pushed up to your remote repository. The great news is you have two option, you can add just one file ready or you can stage all the files at once
git add [name-of-file] //ex git add index.html git add . // adds all the file.
git commit -m , commits your code and stores them in the local repository ready for pushing to your remote repository. The -m lets you write a message which is an important part of every commit, so another developer or yourself knows why you those changes were committed.
git commit -m "Creating a signup form"
The status command gives you the ability to see what phase our filers are in. Which files are modified, which ones are in our staging area or being committed and in which branch we currently are. This is useful when you are working with multiple branches.
The log command returns all of the commits up until now, it shows the author date commit message and branch. This can be useful if you are looking for a particular commit.
A lot of people who worked in SVN style repositories always forget this command when first starting. After you commit your code to your local Git repository you will want to push your code to a remote Git repository so other people can view and pull your code.
When you are working with a remote repository, git pull will get the latest code from the remote repository in to your local repository. This will be more frequent when you starting working with multiple developers.
When working with your code, you shouldn’t really work in the master branch, the reason behind this and I have experienced it. You work really hard one some code push it up and you accidentally break the application you have been working on and then you revert back to the old code and lose all the work and struggle to figure out what caused it, with a branch you can compare changes, test changes without affecting the master (the source of truth)
// creating a branch: git branch [name_of_branch] git branch new_branch // Switching branches: git checkout [name_of_branch] git checkout new_branch // Combining new and switch git checkout -b [name_of_branch] git checkout -b new_branch // Delete a branch git branch -D [name_of_branch] git branch -D new_branch // show all the branches git show-branch
git merge, lets you merge the branch you are working on with the new code into your master branch or from wherever the branch was created from.
git merge [new_branch] // in master branch git merge new_branch
These basic git commands are important to learn! I hope you enjoyed reading about this topic, and I look forward to covering more coding basics in the future.