Enquire Now

Thanks for the like! We're thrilled you enjoyed the article. Your support encourages us to keep sharing great content!

Python

A Simple Guide to Essential Git Commands with Examples

Part II | Part I 

git init

Initializes a new Git repository in your project folder. This creates a hidden .git directory where Git stores all its tracking data.

git init

This command turns the current directory into a Git repository, allowing you to start tracking changes.

git clone

Creates a copy of a remote repository on your local machine. This is useful when you want to work on an existing project.

 git clone https://github.com/username/repo.git

This command downloads the repository from the provided URL and creates a local copy.

git add

Stages changes in your working directory for the next commit. You can stage specific files or all changes at once.

git add file.txt          # Stages a single file

git add .                # Stages all changes in the directory

This prepares your changes to be saved in the next commit.

git commit

Saves the staged changes to the repository with a message describing what was changed.

 git commit -m "Added new feature"

This command creates a new commit with the message "Added new feature".

git status

Shows the current state of your working directory, including staged, unstaged, and untracked files.

git status

This command helps you see which files are ready to be committed and which are not.

git push

Uploads your local commits to a remote repository. This is how you share your changes with others.

git push origin main

This command pushes your local main branch to the main branch on the remote repository named

git pull

Fetches changes from a remote repository and merges them into your local branch.

git pull origin main

This command updates your local main branch with the latest changes from the remote repository.

git branch

Lists, creates, or deletes branches. Branches allow you to work on different features or fixes in isolation.

git branch feature-branch  # Creates a new branch
git branch -d feature-branch # Deletes a branch

The first command creates a new branch, and the second deletes it.

git checkout

Switches between branches or restores files from a specific commit.

git checkout feature-branch  # Switches to the feature-branch
git checkout -b new-branch   # Creates and switches to a new branch

This command helps you navigate between branches or create new ones.

git merge

Combines changes from one branch into another. This is often used to integrate feature branches into the main branch.

git checkout main
git merge feature-branch

These commands switch to the main branch and merge the changes from Feature-branch

git log

Displays the commit history of the repository, including commit messages, authors, and dates.

git log

This command shows a detailed history of all commits in the current branch.

git reset

Undoes changes by moving the HEAD pointer or upstaging files.

 git reset HEAD file.txt  # Unstages a file
git reset --hard HEAD~1 # Resets the working directory to the previous commit

The first command upstages a file, and the second discards all changes since the last commit.

git stash

Temporarily saves changes that you don’t want to commit yet. This is useful when switching branches.

git stash        # Saves changes temporarily
git stash apply  # Reapplies the stashed changes

This allows you to save your work without committing it.

git remote

Manages connections to remote repositories. You can add, rename, or remove remotes.

git remote add origin https://github.com/username/repo.git
git remote -v

The first command adds a remote repository, and the second lists all remotes.

git fetch

Downloads changes from a remote repository without merging them into your local branch.

git fetch origin

This command retrieves updates from the remote repository but leaves your working directory unchanged.

Conclusion

These Git commands are the foundation of version control and collaboration in software development. By mastering these basics, you'll be able to track changes, collaborate with others, and manage your code effectively. Practice these commands in your projects, and you'll soon become a Git pro!

 

 

Sridhar S

Author

Sridhar S

Cloud Admin - Chadura Tech Pvt Ltd, Bangalore

Related Posts

Comments (0)