Git for Beginners: Basics and Essential Commands
What is Git?
Git is a DVCS (Distributed Version Control System) that helps you manage different versions of a software being developed. It is easy to use when you know about it, it really helps in managing different versions of a software and revert to versions when required.
Why Git is used?
Git is used to manage different versions of a software, helps multiple users to code at the same time, make different branches like “main” for stable code and make another branch for a new feature named “new-feature” or something and their you can experiment with all your new ideas, after your experiment is done and you like it you can merge the two branches “main” and “new-feature” into one, and talking about versions of a software we can also revert one version of a software to previous version of the software if required.
Git Basics and Core Terminologies
The basic 4 Pillars of Git are :-
Initialize a git repo in the project, modify the files in current directory.
Stage all the changes using “git add” command for the next step “commit”.
Commit all the changes using “git commit -m “[message]”.
Push all the changes to remote repository from local repository for enhanced sharing.
The core terminologies of git are :-
Repository: A repository (repo) is a folder containing all the important files for the project, there are two types of repositories :-
Local Repository
Remote Repository
Commit: A commit is like a snapshot of your code and a specific time. Each commit has its own SHA-1 HASH for its uniqueness.
Branch: Branches are two codebases in a single repo. It works like one repo is “main” where the latest and most sustainable is and on the other hand is a secondary branch named like secondary or something where you experiment with new features without touch the main branch.
Staging area: staging area is where files wait before commit
HEAD: HEAD is a pointer to current branch or commit where you are currently working.
Common Git Commands
| Command | Use |
|---|---|
| git init | It initializes a new git repository in local folder. |
| git clone [url] | It copies a remote repository in a local repository. |
| git status | It tells which files are modifies, staged or untracked. |
| git add [file] | It moves unsaved changes into staging area for commiting it. |
| git commit -m “[message]” | It commits the changes in the staging area to local history. |
| git push | It uploads all the commits of local file into remote repository. |
| git pull | It fetches changes from a remote repository and merges them into your current local branch. |
| git branch | It lists all branches and helps creating or deleting branches. |
| git checkout [branch name] | It changes between multiple branches. |
| git merge [branch name] | It merges one branch into another branch. |
| git log | It is used to view the history of committed changes in a git repository. |
