For many of us, Version control and especially git seems like a mystery that's very hard to solve. Questions like what is version control, what is git, what does GitHub do, and what is the difference between git and GitHub keep popping up in our heads.
In this article, I will try to answer some of these questions and as a bonus, I will add some git commands that will help you git like a pro. So let's get started.
What is a version control
version control, also known as source control, is the practice of tracking and managing changes to a software code.
Why is version control so important
Version control keeps track of every modification to the code in a special kind of database. If a mistake is made, developers can turn back to check and compare it with an earlier version of the code to fix the mistakes. It's like time traveling, the only one that's possible today.
Version control helps in :
- Maintaining a history of changes
- Branching and merging for efficient collaboration
- Tracing of changes
What is Git
Git is a free and open-source and distributed version control system, originally authored by Linus Torvalds in 2005 and since then, Junio Hamano has been the core maintainer of the project.
Are Git and GitHub the same:
The short and straight answer is NO.
The big answer -
Git is a version control system that lets you manage and keep track of your source code history.
Github is a cloud-based hosting service that lets you manage git repository.
Now that we have answered some of the burning questions, as I promised earlier Let's list out some important git commands.
git config: Tell Git who you are
Tell git who you are - Configure the author name and email address to be used with your commits.
git config --global user.name "Nandan Kumar"
git config --global user.email nandan@example.com
You can also set the user name and email for a specific repository, Run the below commands Just inside the repository
git config user.name "Nandan Kumar"
git config user.email nandan@example.com
git init: Create a new local repository
This command is used to start a new repository. Run the below command to initialize a git repo in the current folder you are in.
git init
Or you can specify the path to the folder you want to initialize the git repo
git init /home/nandan/path/to/repo
git clone: Check out a repository
Check out / Clone a repository by passing the path to the repository
git clone /home/nandan/path/to/repo
You can also provide an URL for a remote server
git clone [URL]
git clone username@host:/path/to/repository
git add : Add files
Add one or more files to the staging
git add <filename> // to add a specific file
git add * // to add all files
git commit: Commit to your changes
Commit changes to the head (but not yet to the remote repository), Make your commit message as meaningful and descriptive as possible. This message will show up in your change history.
git commit -m "your unique commit message"
In order to commit any files, you’ve added with the git add command and any files you’ve changed since then.
git commit -a
git status: Check the status of your repo
This command lists the files you've changed and those you still need to add or commit.
git status
git push: Push your code
Once you commit the changes, the Next thing to do is to push those changes to your remote repository
git push origin master // to push your changes to the master branch
git push origin main // to push your changes to the main branch
git push // to push changes to the branch you currently are in
git remote add origin: Connect to a remote repository
If you haven't connected your local repository to a remote server, you will not be able to make a code push to your remote repository, add the server to be able to push to it.
git remote add origin <server>
In this blog post, We have learned to initialize, clone, make changes, make a commit and finally push our changes to a git repository.
This is a good start, but in order to become a git master, You will have to start practicing these git commands in your day-to-day life.
But that's not all. I will have the task to come up with the next posts in this series, that will have more git commands like branching, updating your repository, tagging and undoing your changes.
Stay tuned, follow me on social media channels, and subscribe to my newsletter here to get updates on my upcoming posts.