A Beginner's Guide to Git

 Introduction:

In the world of software development, managing changes to code is crucial for collaboration and maintaining a record of progress. Git, a distributed version control system, has emerged as the industry standard for tracking and managing code changes efficiently. Whether you're a beginner or looking to enhance your understanding of Git, this blog post will provide you with a brief overview of Git along with some commonly used commands.



What is Git?

Git is a distributed version control system that enables developers to track changes in their codebase. It was created by Linus Torvalds, the same individual behind the Linux operating system. Git allows multiple developers to work on the same project simultaneously and efficiently manage code changes.

Key Concepts:

1.Repository: A repository, or repo, is a collection of files and folders that make up your project. It contains the complete history of changes made to those files and enables collaboration among developers.

2.Commit: A commit represents a snapshot of your project at a specific point in time. It records changes made to files and includes a unique identifier, commit message, and references to parent commits.

3.Branch: A branch is a separate line of development within a repository. It allows developers to work on different features or bug fixes independently without affecting the main codebase.

4.Merge: Merging is the process of combining changes from one branch into another. It integrates the commits made on one branch into another branch, enabling developers to consolidate their work.




Common Git Commands:

               Commands                                                                     Description

 yum install git -y   


apt-get install git -y

 To install Git on RPM-based distributions (e.g., CentOS, Fedora).

 To install Git on Debian-based distributions (e.g., Ubuntu).

 git init .      

To initialize empty repo in current directory

git config user.name "username"       

used to configure user name for our git

git config user.email "email"       

used to configure user email for our git

git add filename    

used to track a file

git add *    

used to track all files & folders

git add .     

used to track all files in current directory (including hidden files)

git add -f filename  

 to track files forcefully

git rm --cached filename      

used to untrack the file

git commit -m "message" filename    

to commit a file

git commit -m "message" .  

used to commit all files which are present in staging area

git log  

used to see the history of the git

git log --oneline   

used to see only commit ID's and messages

git log -1  

used to see the latest commit

git log -3    

used to see latest 3 commits

git log --follow --all filename

used to see the no of commits for a single file

git show commit_id --name-only 

used to see all the commit details along with the file name

 

git show commit_id --stat 

see the histroy of a file (modifications, data add and deletion)

git commit --amend -m "message"   

used to change the commit message for a latest commit

git commit --amend --author "username <mail>" 

 used to change the author of latest commit

git commit --amend --no-edit    

git commit --amend --no-edit    

git reset --hard HEAD~1 

used to delete the latest commit along with the changes

git reset --hard HEAD~3  

used to delete the latest 3 commits along with the changes

git resert --soft HEAD~1

used to delete only commits but not actions/changes

git resert --soft HEAD~3             

used to delete only latest commits but not actions/changes

git revert commit_id     

used to delete a particular commit action and add a new commit for the change

git branch    

used to see the list of branches

git branch branch-name       

to create a branch

git checkout branch-name    

to switch one branch to another

 

git checkout -b branch-name

used to create and switch a branch at a time

git branch -d branch-name   

to delete a branch

git branch -D branch-name  

to delete a branch forcefully

git merge branch

copy the all commits from one branch to another

git cherry-pick commit-id 

copy the single commits from one branch to another

git merge --abort 

used to cancel the merge when conflicts arise

 

git rebase branch   

copy the all commits from one branch to another

git stash      

to delete the changes permanently

git stash save "message"  

to save the stash along with the message

git stash apply

to get back the data again

git stash list 

to get the list of stashes

git stash clear     

to clear all stashes

git stash pop    

to delete the first stash

git stash drop  

 used to delete the latest stash

git stash drop stash@{2}          

used to delete a particular stash

git remote add origin repo-url link local-repo to central-repo

 used to get the linked repo in github

git push -u origin branch-name 

push the code from local to central

git push -u origin branch-1 branch-2  

used to push the code to multiple branches

git push -u origin --all   

used to push the code to all branches at a time

git clone repo-url   

used to get the code from central to local

git pull origin branch 

used to get the changes from central to local

git fetch branch-name

 used to fetch the data from central to local

git fetch --all

used to fetch the changes from all branches in github

git merge origin/branch 

used to merge the changes from central to local

git push -u origin --delete branch-name  

 used to delete the github branch from local

git remote rm origin   

used to unlink the github-repo

git remote rename old -link new-link  

used to change the repo

 

git remove -v   

used to get the linked repo in github


Conclusion:

Git is an indispensable tool for modern software development, offering a robust version control system and facilitating efficient collaboration among developers. By grasping the key concepts and mastering the commonly used commands, you can harness the power of Git to track changes, manage branches, merge contributions, and streamline your development workflow. Embrace Git, and unlock the potential for seamless code management and collaboration in your projects.


Thank you

M. Nishitha (Intern),

DevOps Protectors,

Data Guard Team,

Enterprise Minds.



Comments

Popular posts from this blog

3-Tier Apllication in AWS

Using Trivy for Container Image Vulnerability Scanning in DevOps

Database Backup Software - Feature Study