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 initThis 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.gitThis 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 directoryThis 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 statusThis 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 mainThis 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 mainThis 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 branchgit branch -d feature-branch # Deletes a branchThe 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-branchgit checkout -b new-branch # Creates and switches to a new branchThis 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 maingit merge feature-branchThese 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 logThis 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 filegit reset --hard HEAD~1 # Resets the working directory to the previous commitThe 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 temporarilygit stash apply # Reapplies the stashed changesThis 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.gitgit remote -vThe first command adds a remote repository, and the second lists all remotes.
git fetchDownloads changes from a remote repository without merging them into your local branch.
git fetch originThis 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!


