Git Unleashed: Mastering Commands for Seamless Collaboration and Code Control

Himanshu
6 min readJul 31, 2023

--

In this blog, we’ll learn git from scratch to advance, which every programmer should keep handy and use in order to keep track of the development of a project. There are many version control systems like gitlab, code commit, bitbucket, etc., but our focus is going to be on git.

There are two types of VCS

a. Centralized: subversion, Team Foundation Server

b. Distributed: Git, Mercurial

What is Git?

It’s a version control system, and it can be used to keep track of the changes you’ll make to your project. Now the question is, How?

Git Repository

Before jumping into how we can track changes, we must know what a Git repository is. It is a representation of the project hierarchy, with folders and files structured under it.

A Git repository is a workplace that keeps track of and manages files within a folder.

A fresh git repository must be created everytime we create a new project, app, etc.

we can store as many repos with their own histories and contents as we need.

Also, you should understand that

☆ Every git Project starts with .git directory

☆ It’ll be the origin of project’s data, where git stores the metadata

☆ The default branch is referred to as main.

Installation

you can install git from https://git-scm.com/

The steps are simple just keep on clicking next otherwise go and watch this youtube tutorial which i don’t think will be needed but, here goes the link to do it.

Git Setup Commands

  • $ git init : creates an empty git repository or, reinitialize an existing one.
  • $ git config -- global user.email<email> : initialize user email id for all repos.
  • $ git config -- global user.name “your name” : initialize the user name for all repositories.

These commands will initialize git in your needed folder.

Before getting deeper into commands, let’s discuss the Architecture of git

➢ Working directory

➢ Staging area

➢ Git repository

So, the working directory will be the folder where you’ll be creating your project, and from there, you’ll add your work to the staging area. Once you’re sure about your work, you can commit it to your repository.

Just so you don’t get confused i think there is also need for you to understand that there is a difference between git and github

GITHUB

◦ Web-based platform with git VCS features

◦ provides project and team management features.

◦ good for collaborative work

◦ owned by Microsoft

GIT

◦ VCS for developers

◦ Enables push, pull from another system installation.

◦ Good for individual use.

◦ Open-Source.

So, I hope you got the picture that GitHub is based on GIT, but they have their differences.

A few more commands before we go into version control for you to easily make your way through

  • $ cd .. : to get back to folder
  • $ clear : to clear screen
  • $ ls : list all the folders
  • $ cd file-name/ : to enter into file-name
  • $ mkdir demo : to create a demo directory
  • $ pwd: tells you the present working directory path.

We’re all set now!

Git Commands

if you haven’t created a file yet, then do it by

  • $ git touch filename.txt: It will create a filename.txt file.

Now, you can use git setup commands explained above and then

Start getting status of the current directory you’re in using

  • $ git status: status of files in the staging/unstaging area.

If you’ve decided to make any changes to any file, then add it to staging area using

  • $ git add: updates the current content of the working tree to the staging area.

If you’re sure of the changes, then commit them

  • $ git commit -m “message” : used to commit changes

You can see all the logs using

  • $ git log: it shows all the logs.

So, We’ve now understood the method of initializing a git repository, adding it to the staging area, and committing the changes.

Now, we’ll delve more into the concepts and features that Git offers. Before that, let’s just look at two terms:

Master in GIT

Master is the name of a default branch in Git terminology.

Head

The location of your repository is simply referenced by the pointer head.

Branching in Git

Often known as master or main, the default branch in a Git repository represents the codebase that is stable and ready for production. To work on certain tasks, developers build distinct branches, and once the work is over, they merge the changes back into the main branch.

A branch is a new or separate version of the main repository

  • $ git branch <branch-name>: creates a new branch.
  • $ git branch: shows all the branches.
  • $ git switch: to switch to an available branch
  • $ git branch -d <branch_new>: delete an independent branch.
  • $ git branch -m <New_Name_of_Branch>: to remove an existing branch.

Merging in Git

You can combine the several development lines generated by the git branch into a single branch using the git merge command.

So, it is to merge two or more development histories together.

  • git merge <branch-name> : to merge a branch

Merge Conflicts

If you add something that you’ve removed from the master branch, then it gives you a merge conflict.

Remove the conflict:

remove <<<< ===; that will make it merge with the code without conflicts.

Stashing in Git

It Provides an easy way of stashing these uncommitted changes so that we can return to them

  • $ git stash: used for saving modifications that we’re not ready to commit.

You can roll back to the changes in your repository by stashing all the pending changes.

Commands Used in Stashing

  • $ git stash : to stash uncommitted changes
  • $ git stash pop : move the most recently stored changes in your stash and reapply them to your working copy
  • $ git stash apply: to re-apply stashed changes to your working copy without removing them from stash.
  • $ git stash list: to see all the stashes

Also, if you haven’t added files to the staging area, you can undo the changes by

  • $ git restore < file-name >

Delete a particular Stash

  • $ git stash drop <stash-name> : to drop or remove a particular stash from stashing link
  • $ git stash clear: to remove all the stashes

Check for old commits.

  • $ git checkout <commit-hash> : to view a previous commit

Indeed, We can use the git log command to view commit hashes, we just need the first 7 digits of a commit hash.

  • $ git log --oneline: This will give the hash.

Detached head and Re-attaching Head

  • $ git switch <branch-name> : to reattach head to a particular branch
  • $ git switch --: to reattach the head to a parent branch you were on.

Git Restore

  • $ git restore <file-name> : for undoing operations
  • $ git restore --staged<file-name>: to remove file from staging area.

Git Reset

if you’ve made a couple of commits on the master branch but actually meant to make them on a separate branch instead. To undo these commits, you can use git reset as follows:

  • $ git reset <commit-hash> : reset the repo back to a specific commit
  • $ git reset --hard<commit> : to remove the contents and undo commits.

Note: $ git switch -c branch1 will create a copy of the commits in a separate branch.

Git is a crucial tool for contemporary software development since it offers a reliable and effective version control system. Developers can efficiently collaborate, track changes, and maintain a dependable codebase throughout the development process by mastering these fundamental Git commands and comprehending their use cases. No matter your level of development experience, taking the time to learn Git will unquestionably increase your productivity and code management abilities. Happy coding!

--

--

Himanshu

🚀 Tech Maverick | MERN Stack Developer | Java | Python | Linux | Blogging Magic 📝