Version control or source control is tracking and managing changes to software code. Imagine being able to keep track of all the little changes, additions, deletions that have been made on a project over time. By you and or by other people.
There are several version control softwares, this article gives a summary of git commands and what they do.
Git
Git is used for small-scale and large-scale projects, buy individuals, small teams, large cooperations, etc. It allows developers to collaborate on projects, wherever and whenever they are in the world. I personally believe it’s a game changer.
Where to run git commands
Some IDE’s like Visual Studio have interfaces that allow you to run git commands by the click of a button. Super convenient. However, it is very important to also know how to run these commands from the command line. Or, git bash, which is the Git’s command line.
git init
Initialises an existing directory as a Git repository. The repository is what tracks all changes made to the project. When you first create a project folder on your local computer and intend to track all changes made to it over time, run git init
command on the root of your project.
git clone
Sometimes you create a project from scratch and sometimes you’ll have to work on an already existing project. A project created by someone else. This means you’ll need to have a copy of this project on your PC and the way to get it is using the command
git clone <project-url>
This is basically a download of an existing project from GitHub hub to your computer.
P.S remove the <> when you enter the project url.
git status
This is a helpful command to remember. It shows all the modified files in a working directory. You’re also able to know if you have anything un-commited.
git branch
Gives you back a list of all your branches. Your current branch will have an * before it. Current branch means the branch you’re on.
git checkout -b <branch-name>
Creates a new branch and switches you to this new branch from the branch you were on before.
git checkout <branch-name>
Switches you to an existing branch.
The branch name given must be one that already exists.
git fetch
Downloads all the branches (including their commits and files) from a remote repository onto your local repository. This is a good way to see what others have been doing.
git pull
Fetches and merges / integrates all the content in the remote repository into your local repository.
git commit -m "detailed message"
Saves all the current changes of your project into a commit.
For example if you’ve created a function that makes an api call, retrieves that data and displays it on a web page, then make a commit with a message that describes exactly that.
This will show all the files changed since the last commit.
An important note to remember is that the “detailed message” part has to be as detailed as possible. Other engineers, yourself included will need to know exactly what was happening when those file in the project changed.
This is run right before you run git push
git push
After you make changes in your local repository, you’d want these changes to be reflected in a remote repository. git push
sends your changes to the remote branch where they can be seen by other engineers.
Your changes and a summary of these changes (the message in your commit) will be sent to the remote repository.
git merge
Integrates the changes in a remote branch into your local branch. Basically combines two branches.
git log
Shows your the commit history of your current branch.
git reset --hard <commit>
This command gets rid of everything pushed up to a particular commit. Be careful when running this command. Include the commit at which you want to reset your code to using git log
.
git stash
“Stashes” or saves away any changes you have made locally and allows you continue with your work without the changes and later reapply those changes.
Git has a lot of commands. I have only listed the few that use a lot more than the rest and that I think are helpful to know from the top of your head.
See you in our next lesson 😀