Mastering Essential Git Commands: A Developer's Guide

DevelopmentDecember 25, 2023

Git is a free and open-source distributed version control system designed to handle everything from small to very large projects with speed and efficiency. It's a system that tracks changes to our project files over time. It enables us to record project changes and go back to a specific version of the tracked files, at any given point in time. This change history lives on your local machine and lets you revert to a previous version of your project with ease in case something goes wrong. Git makes collaboration easy. Knowing how to use Git is one of the most important skills for any developer nowadays - and it's a great addition to your resume!

Here we will look at some of the git commands to get you started with git.

  • Check your Git configuration

The command below returns a list of information about your git configuration including user name and email.

git config –l

  • Setup your Git username

git config --global user.name "your-name"

  • Setup your Git user email

git config --global user.email "your-email"

  • Cache your login credentials in Git

You can store login credentials in the cache so you don't have to type them in each time.

git config --global credential.helper cache

  • Initialize a Git repo

The first step to creating your git repository is initializing it. You can initialize a git repo for a new or existing project using the following command:

git init

  • Add a file to the staging area

The next step is to add the files in the project to the staging area. You can add a file using the following command:

git add <filename>

To add all the files in the current directory, use the following command:

git add.

  • Check a repository's status

This command will show the status of the current repository including staged, unstaged, and untracked files.

git status

  • Commit changes in the editor

The common way to commit changes made to your repo is with the –m option which lets you specify a shot summary for your commit message.

git commit -m "your commit message here"

You can add and commit files in one step using the following command:

git commit -a –m "your commit message here"

  • See your commit history in Git

To see your commit history for the current repo, use the following command:

git log

To see your commit history including all the files and their changes, type:

git log –p

  • Remove tracked files from the current working tree in Git

git rm filename

  • Rename files in Git

git mv old file new file

  • Ignore files in Git

Create a .gitignore file and commit it.

  • Rolling back commits

Use the below command to roll back the last commit:

git revert HEAD

To revert an old commit in git:

git revert commit_id_here

  • Create a new branch in git

  • Branch allows teams to work on the same code base in parallel. If you want to add some functionality to a branch without changing the actual code, you can create a branch and if your team likes the changes, you can merge the two branches together. To create a new branch, use the command below:

git branch branch_name

  • Switch to a newly created branch

git checkout branch_name

  • Delete a branch

git branch –d branch_name

  • Merge two branches in git

git merge branch_name

  • Show the commit log as a graph

git log –graph –online

  • Add a remote repository

git add remote https://repo_here

  • See all remote repositories

git remote –v

  • Push changes to a remote repo

When all your work is ready to be saved on a remote repository, you can push all changes using the command below:

git push

  • Pull changes from a remote repo

If other team members are working on your repository, you can retrieve the latest changes made to the remote repository with the command below:

git pull

  • Fetch remote repo changes

This command will download the changes from a remote repo but will not perform a merge on your local branch (as git pull does that instead).

git fetch

  • Merge a remote repo with your local repo

If the remote repository has changes you want to merge with your local, then use the following command

git merge origin/main

  • Push a new branch to a remote repo

git push -u origin branch_name

  • Clone a git repository

The git clone command is used to download the source code from a remote repository. When you clone a repo, the code is automatically downloaded to your local machine. To clone a git repo, use the following command:

git clone <https://url-of-the-repository>

Frequently Asked Questions

  • What are Git commands?

    Git commands are instructions used in the Git version control system to perform various tasks such as initializing a repository, adding files, committing changes, and more.

  • Why are Git commands important?

    Git commands are crucial for efficient collaboration and version control in software development. They enable tracking changes, managing project history, and facilitating teamwork.

  • How can I learn Git commands?

    You can learn Git commands through online tutorials, documentation, interactive platforms like GitHub, and by practicing on your projects.

  • What are some common Git commands for beginners?

    Common Git commands for beginners include git init, git add, git commit, git status, git push, and git pull. These commands are fundamental for basic version control.

  • Are there advanced Git commands?

    Yes, Git offers advanced commands for branching, merging, rebasing, cherry-picking, and more. These commands are useful for managing complex development workflows.

Sharing is caring

Our Work Across Industries

How To Develop Successful SaaS Apps? A Step-By-Step Guide

How To Develop Successful SaaS Apps? A Step-By-Step Guide

Top 5 Platforms to Sell Digital Products

Top 5 Platforms to Sell Digital Products

Essential Visual Studio Code Extensions

Essential Visual Studio Code Extensions

Ready to build
something amazing?

With experience in product development across 24+ industries, share your plans,
and let's discuss the way forward.

Flower, Ireland
Dublin
Ireland
Taj Mahal, India
Ahmedabad
India

CIN#:U72300GJ2015PTC083836

© 2024 Raft Media Systems Pvt Ltd.

Privacy Policy

DMCA.com Protection Status