When I first started learning git, I felt like I was learning what to do without learning why I was doing it and/or what was actually happening behind the scenes.
So with this article, I wanted to give you the information that helped me understand what git is and the commands that I find to be the most helpful when working in a git repository. Please keep in mind that this article is meant to be very brief and it is not my intention to go very deep into the world of git.
Let’s git it!
Unfortunately those are two completely different things.
Git is a version control system which is to manage your source code history.
Simple idea will be you and your friend writing a book and you being able to control different plot development to the story and also able to switch endings of the story base off what your readers prefer.
GitHub is a cloud based version control service. In the book example, GitHub will be like the google doc that allows you to save one version of the current story and letting others to make copy of it and work on their plot development on their local computer.
I found the concept of git and GitHub can be a bit confusing for beginners like me, Hopefully this analogy does not make you get more confused about git and github lol. I’m definitely not smart enough to get it the first day.
First thing first, let’s make sure git is installed in our computers - if not, check the links below for your system:
Before I get started, just wanted to let you know that I will be using Bash shell commands(reference link for Bash). I will also be following a procedure of steps on making a new repository.
-Where Git saves all the data/changes/history of a working project.
-The folder location where the project lives.
-Before committing changes, they need to be staged.
-Git doesn’t save changes until the user actively ‘commits’ changes
-Uploads a committed repository from the user’s hard drive onto GitHub’s server.
-Allows a user to access the most recent changes from the server into the user’s local repository
-Is a command that allows you to copy an existing git repository.
-Branching gives you a way to work on a new feature without affecting the main codebase.
-Merging is when you want to integrate your branch/feature to the master branch.
First we have to make a new directory(fancy way for saying folder) to start our repository/project using mkdir new_project
Now will move into that new directory using cd new_project
We create this as a new git repository by entering git init
From a Git hosting server GitHub:
git clone https://github.com/path_to_repo.git
You will look for the green button saying “Clone or download”:
Then in your terminal you will have to enter:
git clone firstname.lastname@example.org:j3ffh95/IAmGoinToGetCloned.git
It will create a folder of the cloned repository.
Now that we have a repository set up, we need to add some files for git to track:
add all new and modified/edited files in the directory, a period refers to all changes:
git add .
Now that we asked git to add files, we then need to commit those files:
git commit -m "add these changes"
There are two things happening in the above command:
git commit ... ===> is committing the added files.
-m "add these changes" ===> -m will allow you to create a message so your coworkers know what changes are included in this commit.
Once we have added and commited our files , we now need send or push those changes to our repository:
git push origin master
Let me explain to you what the above command is doing:
git push ... ===> send our changes.
... origin ... ===> the location of our repository.
... master ===> the branch name, we will talk more about branches shortly.
Let’s imagine that we are creating a webapp which may have several different features. A good practice is to split the app development into the separate different features. Branches are also important when you are working with others.
Now let's say that one of the features we need to code is a login system …
With git we can make a new branch called login:
git checkout -b login
In the above git command:
- git checkout ===> switch to the following branch name.
- ... -b login ===> -b will change you to that branch
The above command is actually a shortcut one in the sense that it is actually checking out and creating a new branch in one go.
Also if we already have a branch created called login we could switch to it simply by:
git checkout login
After creating this local branch in order to have it remotely we need to push it to our remote repo. We can use the same command that we used earlier.
git push origin login
OK so now let's say we need to get all the updates that our remote repo has. For example if you are creating a project in a team, and all of you are working in different machines at home. And one of your team members makes some changes and then when you start working on the project you want to have the lastest updates then you will need to make a git pull.
- git pull ===> tells git to get the latest version of the repo
Alright so now if our login branch we made earlier is debug-free and we want to merge it to our master branch then we meed to do git merge.
But first you need to be on our master branch so we will run the checkout command:
git checkout master
Then we can merge the new branch using the merge command:
git merge login
So now the master branch will have all the login feature from our login branch. This will work but sometimes when you are working other you may get a merge conflict. If you want to learn more about merging you can go here.
Last but not least is undoing changes. There are going to be occasions that you will need to undo changes to the local repo. For example we would like to remove some changes we did to a file called index.html, then we would need to run the checkout -- command :
git checkout -- index.html
ALso this command is very usuful when someone edits or erases any files in your repo, by using this command it goes back to your last commit.
And We are done!(well for the basics 😅)
These are some awesome resources to get to know more about git: