Steps to Understand GIT Commands (beginner's guide)
Introduction to Git version control
Table of contents
Little history lesson about GIT
Git was created by Linus Torvalds about 10 years ago. The reason behind creating GIT was he hated doing source control management, imagine your day without version control or source management like, " who coded that and who made changes on that method" Terrible right! so, Linus thought of creating a systematic environment that can take care of changes and code that is made by several of my teammates.
Git has its own learning curve, I faced many problems at the beginning of my programming journey, as nowadays Git is like a global standard like anybody who can code should know Git, and know how to manage code with multiple teammates and should not mess up the whole codebase, cause I've created many issues for my senior teammates but they were very helpful to correct me, and they've explained me the best practices for git, and I'm going to tell you all that.
Repository
Repository or (repo) in Git is a collection of files and folders where your whole codebase is located. You can create Unlimited public/private repositories and can create Unlimited Branches.
Branch
Branch in Git is the same as you're thinking right now I suppose, Let's take an example of a tree and its branches with leaves.
Example: | |
Trees | Repositories |
Branch | Branch |
Leaves | People |
The repository is like a tree where everything happens where fruit will emerge and grow and at the end, it will ripe.
The branch is for different things happening at a different pace, but everything is related to the tree, for example, you cannot expect to have an apple on one branch and mango on another, cause the tree is of the same codebase, either it will have mango or apple.
you can perform different tasks on different branches of the same repository and both the task will eventually be merged into one sample master branch which is the root of all branches
And lastly, Leaves are the people that are connected to that specific repository and you can have an unlimited number of people (leaves) to your branch Repository.
here are some commands that you'll be going to use at the beginning of any project or any codebase. they are in no specific order
\==> Note: The staging area is the area where all the files are ready to be committed
- git clone
git clone " < URL > "
To clone the global repository on your local machine.
- git config
git config —global username “Harshit Saxena”
git config —global.email “harshit@harshitsaxena.com”
This is to tell git who you are your username, email, address, etc. this information is used during your commits.
- git add
git add < filename >
Files that you want to commit to your repository or ready to be staged
git add --all
- To add all the files to the staging area
git add “*.txt”
To add all the text file
git add < directory >/
To add the specific directory to be staged
git commit
commit command is used to send your code to your local repository
- m ==> message
git commit -m "write your comment here describing your commit"
- git push
git push origin master
This is used to push your code from the local repository to the Global repository
- git pull
git pull origin master
To get all the code from the Global repository to your local repository
git status
- To check the status of all the files
git branch
- To check the branch that you are working in
git checkout -b < branchname>
- To make a new branch and switch to a newly created one
git checkout < branchname >
- To change the working branch
git merge
- To merge different branches to the active branch
git log
- To check all the commits logs
git grep " specific content "
- To search into the working directory "this"
---> These are some commands that will be helpful in your git journey, So in the end I must say to make repositories you will mess up many times but you will learn from your mistakes, that is what really counts. Believe me, I've made mistakes on client repositories in the first week of my job, but learning from your mistakes makes you better at anything. So keep on trying new things.
But try making mistakes on your local repository rather than on the client's repository.
You can find my GitHub profile on my Hashnode Profile.
Summary
Git is a very powerful tool for any programmer (experienced / beginner), so I strongly encourage you to get your hands dirty with git. Make a GitHub profile and start coding. Feel free to add your GitHub profile in the comment section. I definitely get back to you with some special tricks that might speed up your process 😉.