Git is a distributed version control system (VCS) that empowers you to track changes made to project files. It streamlines code management and team collaboration. Whether you’re an individual developer or a team member, learning Git simplifies version control and enhances effectiveness.
The main benefits of Git are:
A Git project consists of three major sections: the working directory, the staging area, and the git directory.
The working directory is where you add, delete, and edit files. Then, the changes are indexed in the staging area. After you commit your modifications, a snapshot of the changes will be saved in the directory.
After Installation We need To configure git using git bash
git config --global user.name 'YourName'
git config --global user.email 'YourEmail'
git init
git init [project name]
git add file1.txt
git commit -m "Add new feature"
git status
git log
git diff file1.txt
git diff commit1 commit2
git rm file1.txt
git mv file1.txt file2.txt
git mv file1.txt new_directory/
git clone https://github.com/username/my-project.git
For example, you want to push changes from the local repository called “main” to the remote repository named “origin”:
git push origin main
git pull origin master
git fetch origin
git remote
git remote add origin https://github.com/username/origin.git
A Git branch is like a copy of your project where you can work on new stuff without messing up the original. When you’re done, you can merge the changes back in.
git branch
git branch [new_branch]
git checkout [new_branch]
Switched to branch ‘test’
Now, in that new development branch, we can create as many code modifications as we want without having to change anything in the main one. As we can see, it keeps the program organized for new code inclusions.
git branch
There is something we need to keep in mind if we want to make a new development branch. First, we need to commit to the main branch for Git to understand what the master branch is. If we do not do this, we will get an error. So first, commit and then create the development branches.
git branch -d [branch_name]
However, in order to do this, we must not be located on the branch we want to remove. So in this case, we move to the master branch and from there delete the branch we just created:
git checkout master
git branch -d test
Finally, there comes a point where we have made many modifications to a development branch. And it becomes stable, so we want to link it to another development branch. For that, there is the merge command.
First, locate the development branch to which the second branch is to be attached. For example, we will attach the test branch to the master branch. Then, we have to place ourselves in the master branch and merge with the command:
git merge [branch]
As you can see the basic Git branch functions are pretty easy. You just need to know the fundamentals, and try to keep your management clean.
Git Stash is a command in the Git version control system that allows you to temporarily save changes in your working directory that you do not want to commit immediately but also do not want to discard.
git stash
After running git stash, your working directory will be reverted to the state of the last commit. This means your changes are no longer present in your working directory.
git stash apply
git stash apply will apply the most recent stash and keep it in the stash stack
git stash pop
git stash pop will apply the most recent stash and remove it from the stash stack
git stash list
git stash drop
or
git stash clear // for clearnig all stashes
Here are some common use cases for git stash:
git it stash is a valuable tool for managing your work in Git and maintaining a clean and organized version control history