Git commands that we often need and use as developers

Ever worked on a project and wished you could rewind time to an earlier version? Or struggled to keep track of changes made by yourself or your teammates? Fear not, developers, for version control is here to save the day!

Version control is the absolute hero in the software development world. It’s like a magic time machine that lets you track changes to your code, revert to previous versions if needed, and collaborate seamlessly with others. Imagine working on a massive project with a distributed team – version control ensures everyone’s on the same page, avoiding code conflicts and ensuring a smooth development flow.

Among VCS tools, Git reigns supreme. Its ease of use through the command line (CLI) makes it a favorite for developers worldwide. Mastering a few essential Git commands becomes second nature, just like having a solid vocabulary for clear communication. Ready to unlock the power of Git? Buckle up as we explore the fundamental commands that will empower you in your development journey!

Basic Commands:

  • git init: Creates a new Git repository in the current directory.
  • git clone <remote repo URL> Creates a local copy of a remote repository.https://github.com/git-guides/git-clone . eg: git clone https://github.com/koirpraw/rest-api-nodejs-mongodb-notes-app.git
  • git config: Sets up various Git configurations, like your name and email address.
  • git status: Shows the current state of your working directory and staging area.
  • git add [file]: Adds a file or directory to the staging area, which tells Git that you want to include those changes in the next commit.
  • git add . : Adds all the changes tracked on the active branch
  • git commit -m "[message]“: Creates a commit, which is a snapshot of your project at a specific point in time. The message describes the changes made in that commit.

Branching and Merging:

  • git branch: Lists all branches in your local repository or creates a new branch.
  • git checkout [branch_name]: Switches to a different branch.
  • git checkout -b [branch_name] : create and switch to a new branch
  • git merge [branch_name]: Merges changes from another branch into the current branch.

Remote Repositories:

  • git remote <Your Remote Repo URL> Adds a remote repository to your local repository (e.g., a repository on GitHub).
  • git remote remove origin: Removes remote repository configured as the origin for remote repo. ( You might wanna do this right after you clone a third part repo to play around or make changes for your own self and add it to your own remote repo instead)
  • git pull: Fetches changes from the remote repository and merges them into your local branch.
  • git push: Uploads your local commits to the remote repository.

Viewing History:

  • git log: Shows the commit history of your repository.

Additional Commands:

  • git diff: Shows the difference between your working directory and the staging area (uncommitted changes) or between the staging area and the latest commit (staged changes).
  • git stash: Temporarily saves your uncommitted changes away, allowing you to work on something else without losing your progress.
  • git rm [file]: Removes a file from your working directory and stages it for deletion in the next commit.
  • git rm -r –cached <file/folder name>: removes the specified folder from git and updates repo when pushed eg: git rm -r --cached node_modules
  • rm -rf .git: to remove git entirely from your project ( You might want to do this if you are going to be combining multiple projects under a same directory and add git tracking for all of them.you can not have multiple git tracking in individual projects . Instead you can remove the git from the projects if they already have them and instead initialize git with command git init , for the entire directory eg: combining front end and backend separate projects in a fullstack application packaged under same git project directory)

These are just a few of the many Git commands available. There are many resources available online that provide more comprehensive Git tutorials, including

https://www.atlassian.com/git/tutorials.

https://github.com/git-guides

https://git-scm.com/

Praweg

A Curious and Creative fella with knack for coding.

Leave a Reply

Your email address will not be published. Required fields are marked *