🧑💻Git config
git config
is used to set and view configuration options for your Git environment. These configuration options control various aspects of how Git behaves, such as user information, text editors, default behaviours, and more.
We will use git config to set our username and useremail.
- Set user information:
git config --global user.name "Your Name"
git config --global user.email "youremail@example.com"
➡️Git Config is also used to
2. Set default text editor (e.g., for commit messages)
3. Set automatic rebasing of the current branch when pulling
🌿Git Branch
It shows in which branch you're currently working.
Created one file named feature1.txt inside the dev branch.
Added and committed the changes in the dev branch.
Added some more lines to the feature1.txt
And committed to the dev branch.
📍Git Revert 🔙
git revert
is a command used to create a new commit that undoes the changes made in a previous commit. Unlike git reset
, which removes commits from the history entirely, git revert
creates a new commit that effectively "reverses" the changes introduced by a specific commit while keeping a clean history.
Since we do not want the second and third line from the feature1.txt, here we will git revert, git revert <commit_id>
📍Git Reset
git reset
is a powerful command that allows you to move the HEAD pointer and change the current branch's history. It is used to reset the state of the repository, and it comes with different options, which can be a bit confusing for newcomers. The three primary modes of git reset
are "soft," "mixed," and "hard.
So while pushing the files to git add, by mistake also pushed the passwords.txt file which has some of the tokens and passwords. So to overcome we will use git reset <commit_id> --hard
--hard
: This mode is the most aggressive. It moves the HEAD pointer to the specified <commit>
and resets both the staging area (index) and the working directory to match the state of the reset target. Be cautious when using --hard
, as it can result in permanent data loss for the changes that were not committed or stashed.
➡️Reminder before using git reset --hard
Always be cautious when using git reset --hard
since it can result in data loss. It's a good practice to create backups or use Git branches to experiment with changes before performing a hard reset, especially when collaborating with others.
📍Git Merge
git merge
is a Git command used to combine the changes from one branch into another. It integrates the work done on different branches, creating a new commit that represents the merged state. This allows developers to bring new features or bug fixes from separate branches into the main branch of the repository.
So after working on the dev branch now it's finally done. Now we want to push changes to the main branch. First, we will check out to the branch git checkout <Branch_name>
where we want to merge the changes. Then use the following command git merge <Branch_name>
this will merge the entire branch to the current branch.
📍Git Rebase
git rebase
is a Git command used to integrate changes from one branch onto another branch. Unlike git merge
, which creates a new merge commit to combine changes, git rebase
rewrites the commit history by moving or applying the commits from one branch to another.
➡️Difference between Git Merge and Rebase
The main difference between git merge and git rebase is that git merge is a way of combining changes from one branch (source branch) into another branch (target branch) where as git rebase is a way of moving the changes from one branch onto another branch.