A Beginner’s Guide to Version Control Basics
Introduction
Understanding how to utilize Git efficiently is crucial in the world of collaborative development today. Pushing your first file to GitHub and working with branches are two fundamental Git chores that I'll go over in this post. These methods will help you become more confident with version control, regardless of whether you're a student, intern, or someone who is just brushing up on your Git skills.
-- Initialize a Git repository:
git init
This command creates a hidden .git/ folder in your project directory. It marks the folder as a Git repository. From this point, Git starts tracking changes in files.
-- Create a file (for example, a README)
echo "# My First Git Project" > README.md
In Linux with the help of echo command we can create a file and add text into it or we can also use "touch" and "cat" command to create a file .
-- Check the status of your repo:
git status
You’ll see that the README.md file is untracked. That means Git sees it but hasn’t started tracking it yet.
-- Add the file to the staging area:
git add READM.md
What is the staging area? The staging area is like a clipboard where it keep tracks of your file — it holds your changes temporarily before committing them. You can stage multiple files and then commit them all at once.
-- Commit the file
git commit -m "Initial commit: added README"
This permanently records the staged changes to the Git history.
-- Command to check git log
Git log command helps us to see various commit that we have done till now in this branch .
-- Connect to your GitHub repository:
Recommended by LinkedIn
git remote add origin git@github.com:kunal1601/Gitnew.git
What it does:
-- Push the code to GitHub
git push -u origin master
In the above image you can see that we have pushed our code from local system to centralised GitHub repository .
2. Branching out - Create and Merge Branches
In this section we will understand how to create a new branch and make some changes into code and then merged it into the main branch.
-- Create and switch to a new branch:
git checkout -b dev
git add README.md
git commit -m "Updated README with dev branch"
-- Switch back to the main branch:
git checkout master
-- Merge the changes from the feature branch:
git merge dev
This brings the changes from dev branch into master branch.
Wrapping up
For developers, mastering Git is like gaining an edge over others. These basic tasks, which include initializing a repository, pushing code, and maintaining branches, set the foundation for smooth project administration and collaboration. Take it one step at a time if you're unfamiliar with Git, and don't be scared to try new things. You get more version control confidence with each push, pull, and merge.
Vimal Daga – Thanks for your guidance while learning Git!