Git and git commands
What are git and git advantages and commands their uses? #devopsjourney #devopscommunity #devopstools #devopsenginee #gitcommands #git
GIT
Git is a distributed version control system that allows multiple people to collaborate on a project while keeping track of changes.
Git is a type of Source Code Management (SCM) tool. It helps in managing and tracking changes to source code files. Git provides features like version control, branching, merging, and collaboration, making it a popular choice among developers.
Uses:
Tracking changes efficiently
Enabling collaboration among team members
Allowing for easy branching and merging
Facilitating faster and more efficient development workflows
Branching and merging are fundamental concepts in Git that allow developers to work on different features or fixes simultaneously and merge their changes back into the main codebase. Let's explore the process of branching and merging in Git in detail:
Branching:
Branching allows developers to create independent lines of development within a Git repository. This means that different developers can work on separate branches without affecting the main codebase (usually referred to as the "master" or "main" branch).
To create a new branch, developers can use the command git branch <branch-name>. This will create a new branch with the specified name based on the current branch.
Developers can switch to a different branch using the command git checkout <branch-name>. This allows them to start making changes within the context of that branch.
Each branch has its commit history, allowing developers to make changes, add new commits, and push them to the remote repository without affecting other branches.
Branches can be used to develop new features, fix bugs, experiment with code changes, or work on specific tasks. Once the changes made in a branch are complete and tested, they can be merged back into the main branch.
Merging:
Merging is the process of integrating changes from one branch into another, typically merging a feature branch back into the main branch.
To merge changes from a branch into the current branch, developers can use the command git merge <branch-name>. This command combines the changes made in the specified branch with the current branch.
Git uses a merge algorithm to automatically merge changes whenever possible. However, conflicts can occur when changes overlap or contradict each other. In such cases, Git notifies the developer to manually resolve the conflicts.
Resolving merge conflicts involves examining the conflicting files, identifying the conflicting changes, and applying the necessary modifications to create a coherent merged result. Once the conflicts are resolved, the changes can be committed and the merge can be completed.
It's important to note that when merging a branch into the main branch, it is recommended to perform a code review and testing before merging to ensure the quality and integrity of the codebase.
Overall, the branching and merging process in Git allows for concurrent development, easy isolation of features or fixes, and controlled integration of changes into the main codebase. It provides a flexible and efficient workflow for version control, enabling collaboration among developers and ensuring a smooth integration of new features or bug fixes. By understanding and effectively utilizing branching and merging in Git, development teams can streamline their development process and maintain a stable and reliable codebase.
Basic Git Commands
- To check it is present or not
git --version
- To install git in Linux
Sudo yum install git
- To initialize the git
git init
- To configure the credentials
git config --global user.name ''username"
git config --global user. email "email address"
- To check the status of the files in git
git status
- To add files from the working directory to the stage area
git add <file name>
To add all files at a time
git add.
- To commit file from stage to local repo
git commit -m " message which you want to give it"
- Git is already tracking the file and if you made some changes to it and want to commit then use this command.
git commit -a -m "commit message"
- To check files in git
git ls-files
- To check the log info
git log
git log --online
- To reset the changes
git reset --hard commit id -> changes will be removed from everywhere like working directory, staging area and local repo
git reset --soft commit id --> changes made in that commit will be removed from the local repo
git reset --mixed commit id -> changes made in that particular commit id will be removed from the stage area and local repo. and only present in the working directory
git revert commit id
The changes done in that particular commit will be undone (re reverse)
- To remove file
git rm filename
Git Branch commands
- To create a feature branch from the main/master branch
git branch <branch name>
- To switch branches from one to another
Git checkout <branch name>
- To create a new branch and switch to it at a time
Git checkout -b <branch name>
- To rename the branch name
Git branch -m <old branch > <new branch name>
- To check branch has been merged or not
Git branch --merged
- To check branch has been not merged
Git branch --no-merged
- To delete branch
git branch -D <branch name>
Git Merge commands
- To merge the change from one branch to another branch
git merge <branch name>
Git rebases <branch name> ---for linear log history/ similar to merge but rewrite the log history.
Git stash commands
You have the scenario where you have been asked by your lead or manager to work on a priority task but you are already working on some task. So in this case you can keep the files that you are already working in the temporary shell by using the git stash command. let's see
- To keep files in a temporary shell for some time
git stash
- To know the stash list
git stash list e.g stash@{0} it is the stash number
- To know the files in the temporary shell
git show stash@{0}
- To get back files from the stash is in two ways.
1. git stash pop stash@{0} -> NO files will be present in temporary shell
2. git stash apply stash@{0} -> files will be present in the team shell as well.
- To keep untracked files as well in a temporary/virtual shell.
git stash -u
- To delete stash
git stash drop stash@{0}
Subscribe to my newsletter
Read articles from vaheeda begum sheik directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by