Skip to main content

Command Palette

Search for a command to run...

Git for beginners: basics and essential commands

Updated
5 min read
Git for beginners: basics and essential commands

Git is an open-sourced distributed version control system (DVCS) to manage the collaborative or personal coding files effectivity. Git installed in the local system and manages from the local with the remote repository. In the server side, modern management system like Github, GitLab manages the coding files and extends to deploy those code in the server destination like CI/CD approaches.

Why git is used

Git's main ideas include its distributed setup, where developers have the entire project history on their local machines, a repository as the main storage, commits as snapshots of the project, and branching and merging for working separately and then combining changes.

How git works

Git Workflow

Git workflow, from https://madewithml.com/courses/mlops/git/

To start working on git, you need to install and configure the git in your local system. To understand the git as how it works and the role of this .git folder. Refer this article: Inside Git: How It Works and the Role of the .git Folder

Starting from git init to initialize the local repo → git add . to stages all the local changes → git commit to commit all the local changes with message → git push to store all the local changes to server → git pull to take other’s committed file in to the local system to keep the system at sync.

Git basics and core terminologies

  • Repository (Repo): A project's storage location, containing all files and their complete revision history.

  • Commit: A snapshot of your files at a specific point in time. Each commit has a unique ID (SHA) and a description of changes.

  • Branch: A parallel version of the repository. It allows you to develop features or fix bugs in isolation from the main codebase (usually called main or master or dev).

  • Staging Area (Index): A "buffer" zone where you prepare changes before they are officially committed to the history.

  • HEAD: A pointer to the current branch or specific commit you are currently working on.

  • Remote: A version of your repository hosted on a server (e.g., GitHub, GitLab) that allows for collaboration.

  • Merge: The process of combining changes from one branch into another.

  • Clone: Creating a local copy of a remote repository on your computer.

  • Fork: A personal copy of someone else's project on a remote server, used for contributing to open-source projects.

Basic git workflow

To manage changes effectively, Git follows a specific workflow involving three main trees: the Working Directory, the Staging Area, and the Repository.

  1. Initialize/Clone: Start a new repo with git init or copy an existing one with git clone [URL].

  2. Modify: Make changes to files in your Working Directory.

  3. Stage: Use git add [file] to move changes to the Staging Area.

  4. Commit: Use git commit -m "message" to permanently save staged changes to the Repository history.

  5. Push/Pull: Share your work by sending commits to a remote repo using git push, or update your local repo with others' changes using git pull

Common git commands

git config --global user.name “[firstname lastname]” : set a name that is identifiable for credit when review version history

git config --global user.email “[valid-email]” : set an email address that will be associated with each history marker

git init : initialize an existing directory as a Git repository

git clone [url] : retrieve an entire repository from a hosted location via URL

git remote add [alias] [url] : add a git URL as an alias

git fetch [alias] : fetch down all the branches from that Git remote

git merge [alias]/[branch] : merge a remote branch into your current branch to bring it up to date

git push [alias] [branch] : Transmit local branch commits to the remote repository branch

git pull : fetch and merge any commits from the tracking remote branch

git status : show modified files in working directory, staged for your next commit

git add [file] : add a file as it looks now to your next commit (stage)

git reset [file] : unstage a file while retaining the changes in working directory

git diff : difference between file about what is changed but not staged

git diff --staged : difference of what is staged but not yet committed

git commit -m “[message]” : commit your staged content as a new commit snapshot

git branch : list all the branches. a * will appear next to the currently active branch

git branch [branch-name] : create a new branch at the current commit

git checkout : switch to another branch and check it out into current working directory

git merge [branch] : merge the specified branch’s history into the current one

git log : show all commits in the current branch’s history

git stash : Save modified and staged changes

git stash list : list stack-order of stashed file changes

git stash pop : write working from top of stash stack

git stash drop : discard the changes from top of stash stack

git rebase [branch] : apply any commits of current branch ahead of specified one

git reset --hard [commit] : clear staging area, rewrite working tree from specified commit

git log branchB..branchA : show the commits on branchA that are not on branchB

git log --follow [file] : show the commits that changed file, even across renames

git diff branchB...branchA : show the diff of what is in branchA that is not in branchB

git show [SHA] : show any object in Git in human-readable format

git rm [file] : delete the file from project and stage the removal for commit

git mv [existing-path] [new-path] : change an existing file path and stage the move

git am <patch_file> : applies the specified patch to the current branch.

git am --abort : cancel the entire operation and revert your branch to its original state before you started the am command.

More from this blog

S

Somnath Sahu

14 posts