Git is a powerful and widely-used version control system that allows developers to track and manage changes to their code over time. Whether you’re working on a solo project or collaborating with a team, Git can help you keep your code organized and ensure that everyone is working on the latest version.
One of the key features of Git is its ability to track changes to files over time. When you make a change to a file, you can commit that change to the Git repository. Each commit is like a snapshot of your code at a particular point in time, and you can easily revert back to any previous version of your code.
In addition to tracking changes to individual files, Git also allows you to create branches. A branch is like a separate copy of your code that you can work on without affecting the main version of your code. This is particularly useful when you’re working on a new feature or bug fix and want to test it out before merging it into the main codebase.
Another key feature of Git is its ability to handle conflicts. When multiple people are working on the same codebase, it’s common for different people to make changes to the same file. Git makes it easy to resolve these conflicts by allowing you to see the changes made by each person and choose which changes to keep.
Git also has a large and active community, which means that there are many resources available to help you learn how to use it. Whether you’re a beginner or an experienced developer, there are tutorials, videos, and documentation available to help you get started with Git and master its many features.
If you’re a developer and haven’t used a version control system before, git is a great place to start. Its easy to use and has a wide range of features that will help you manage your code more effectively. Git allows you to collaborate more efficiently and keep track of the changes you make, which makes it a valuable tool for any developer.
In conclusion, Git is a powerful, flexible, and widely-used version control system that is used by developers around the world. With its ability to track changes over time, handle conflicts, and create branches, it can help you manage your code more effectively and collaborate with others more efficiently. Whether you’re working on a solo project or part of a team, Git is a valuable tool for any developer.
Installation
Linux : sudo apt-get install git -y
Cent OS: sudo yum install git -y
Windows : https://git-scm.com/download/win
Note: If you are using Windows OS and Installed git then You will have git bash installed and you can use git bash to perform all the tasks . You can run most of the Linux commands in the git bash
Configure metadata
ONE TIME ACTIVITY
$ git config user.name "Devendra Suthar"
$ git config user.email "Youremail"
check the config
$ git config --list
Basic Commands
First of all, navigate to your project folder/directory either using git bash or Windows GUI
Create a project folder / Subfolder
Steps
Step 1: Create a repo
git init
Initialized empty Git repository in C:/workspace/git/testrepo/.git/
(.git is a hidden directory)
Step 2: Write a code file
touch test1.html
vi test1.html
Step 3: Add a code file to git
git add test1.html
Step 4: Commit a code file to git
git commit -m”first git commit”
Step 5: Look at the Committed file / Logs
git log
check what is committed
git show commit id (example commit id: e50a0462f929e1f60c39fca8cec4e5de2f8a1898)
git status
GIT Workflow
Git Add vs Git Commit
Add: Workspace to Staging / Index (Temp area)
Commit: Staging to Repo
git add
is used to stage changes in your working directory to be ready for a commit. It adds the changes to the “index,” which is like a staging area for the changes you want to commit.
git commit
is used to take the changes that are in the index and create a new commit with those changes. The commit includes a message describing the changes made.
In summary, git add
stages the changes while git commit
saves the changes permanently to the repository with a commit message.
How to commit only selective files
Step 1: Create a file
touch test4.html
Step 2: Check the status of a file
git status
Step 3: Add a code file to git
git add test4.html
Step 4: Check the status of a file
git status
Step 5: Commit only selective file
git commit -m” only test4.html ” test4.html
How to add multiple files to git
Step 1: Check the status of all pending file
git status
Step 2: Add all files to git
git add –all
How to commit a directory
Step 1: Create a directory
mkdir src
Step 2: Check the status if git is able to detect an empty directory
git status
ls
Step 3: Create a file inside the directory
touch src/readme.txt
Step 4: Check the Status and commit the directory
git status
git add .
git commit -m”all”
How to delete a file
Step1: Create , add and commit in single line
touch test5.html;git add .;git commit -m”file5″
(Commit will add file from local workspace to repo)
Step2: delete file
git rm test5.html
(delete from local workspace)
Step3: Commit
git commit -m”delete file5″
(commit will delete file from repo)
Step4: Check Status
git status
git log –oneline
Step5: bring back the deleted file
git checkout commitid
Step6: Undo the restore process
git checkout master
How to rename files
Step1: Create , add and commit in single line
touch test6.html;git add .;git commit -m”file6″
(Commit will add file from local workspace to repo)
Step2: Rename file using mv command
git mv test6.html test66.html
Step3: Check status
git status
How to move a file
Step1: Create , add and commit in single line
touch test7.html;git add .;git commit -m”file7″
(Commit will add file from local workspace to repo)
Step2: Move file using mv command
git mv test7.html src/test7.html
Step3: Check status
git status
Step4: Commit
git commit -m”Move file”