Essential Git Commands: A Comprehensive Guide for Efficient Version Control

ArturArtur
9 min read

Git is one of the most crucial tools you need to master in programming. Whether you're just starting out as a coder or you're a seasoned developer, understanding Git is essential for managing complex projects with ease and collaborating effectively with others in the coding community.

Think of Git as a magic diary for your code. Every time you make a change, Git carefully records it. This means if you ever need to revisit an earlier version or figure out what modifications were made, Git provides all the answers. It’s like having a personal assistant who keeps track of every change in detail, which is incredibly helpful for both solo projects and team collaborations. By learning Git, you can ensure that your projects are organized and that collaboration is seamless, much like a well-coordinated team in a relay race passing the baton without a hitch.

Primary statuses in Git

  • Untracked - file is untracked if it is new to your repository and has not yet been added to the Git database; it wasn't previously tracked. Git does not track these files until you explicitly tell it to do so using git add. Until then, Git ignores these files, meaning they won't be included in your commit snapshots until you decide to track them.

  • Modified - file is modified when it has been changed but the changes are not yet recorded in your Git database. Git recognizes that the file is different from the last snapshot stored in the repository (from the last commit). However, these changes are not yet staged for the next commit. This file was previously tracked by Git.

  • Staged - when a file is staged, the current version of the file has been marked to go into your next commit snapshot. This means that you've used the git add command to promote the modified file to the staging area. Staging a file tells Git that you've finalized the changes on that file, and you're ready to store this version in your next commit. This allows you to continue making changes to the working directory; only the staged version will be included when you commit.

  • Unmodified (Clean) - files that have not been changed since their last commit are considered unmodified or clean. Git tracks these files, but since there have been no changes, there is nothing new to record or update. They are in a stable state.

  • Deleted - files are considered deleted when they have been removed from your working directory but were part of the repository in previous commits. If you delete a file and then run git add on that deletion, Git stages the deletion to be committed. This action tells Git that you intend to remove the file from the repository.

Basic Snapshotting

  1. git status - this command displays the state of the working directory and the staging area. It lets you see which changes have been staged, which haven't, and which files are not being tracked by Git. It's a handy way to check what's going on in your repository before moving on to the next steps in your workflow.

  2. git add [file]||git add .(dot represents current directory) - adds a file's current content to the staging area, making it ready for the next commit. Use git add . to add all modified and new (untracked) files to the staging area. This command is crucial for selecting which changes you want to commit.

  3. git commit -m "commit message" - captures the state of the staged changes and creates a new commit with the provided message. This message should be a brief description of the changes included in the commit. Committing is like taking a snapshot of your project's currently staged changes.

    Shorthand:git commit -am "Comment" - used in Git to simultaneously add changes from all tracked files to the staging area and commit them with a message. Does not add new (untracked) files to the staging area.

  4. git diff||git diff --staged - shows the differences between files in your working directory and the index (staging area) or between the staging area and the last commit. git diff by itself shows changes that are not yet staged, while git diff --staged shows what has been staged so far but not yet committed.

Branching and Merging

  1. git branch - this command is used to manage branches within your Git repository. It can list, create, delete, or rename branches. Branches are effectively independent lines of development that can be created off the main project to develop features, fix bugs, or experiment without affecting the stable version.
  • git branch [branch-name] to create a new branch;

  • git branch to list all local branches;

  • git branch -d branch-name to delete a branch;

  • git branch -m old-branch-name new-branch-name to rename a branch;

  1. git checkout - used to switch between different branches or to restore files in your working directory. It can also be used to checkout tags or specific commits.
  • git checkout [branch-name] to switch to another branch;

  • git checkout -b [new-branch-name] to create a new branch and switch to it immediately;

  • git checkout [commit-hash] to view the state of the repository at the time of the specified commit, detaching the HEAD;

  • git checkout -- [file-name] to discard changes in the working directory for specific files, restoring them with the version from the HEAD (the last commit on the current branch);

  1. git merge - combines the changes from one branch into another, which can be the current branch or another branch you specify. It is typically used to integrate the work done in separate branches.
  • git merge [branch-name] to merge the specified branch into the current branch;
  1. git log - displays a list of recent commits in the current branch’s history. It can be customized to show the history in different formats and detail levels. You can use VSCode extention called GitLens to better visualize logs.

  2. git cherry-pick [commit-hash] - allows you to pick a single commit from one branch and apply it to another branch. This can be extremely useful for porting bug fixes or feature updates without needing to merge entire branches, which might bring in unwanted changes.

Sharing and Updating Projects

  1. git pull [remote] [branch] - this command fetches the latest changes from the specified remote branch and automatically merges them into the current branch of your local repository. Example: git pull origin main would fetch and merge changes from the main branch of the origin remote into your currently checked-out branch.

  2. git push [remote] [branch] - uploads your local repository content to a remote repository. This command is used to share your commits with others by updating the remote references along with the associated objects. Example: git push origin main would push your local main branch commits to the main branch at the origin remote.

  3. git remote - this command allows you to view, add, and delete the connections to other repositories. It is often used to set up a new remote URL that you can push to and pull from. Example: git remote add originhttps://github.com/user/repo.git adds a new remote named origin with the URL of the repository.

  4. git fetch - downloads commits, files, and refs from a remote repository into your local repo. Example: git fetch origin retrieves new work done in the remote called origin but doesn’t merge it with any of your work.

  5. git clone - is used to make a copy of an existing Git repository into a new directory on your local machine. Example: git clonehttps://github.com/user/repo.git clones the repository from the provided URL into a new directory on your local machine named after the repo.

Undoing Changes

  1. git revert [commit-hash] - used to create a new commit that undoes the changes made by previous commits. It is a safe way to undo changes, as it does not alter the existing history.

  2. git reset - used to unstage staged changes, revert committed changes, and even change the current branch's commit history. Example: If you want to discard all local changes and commits since a certain commit, git reset --hard [commit-hash] will revert your repo to that state.

  3. git restore [file-name] - used to restore files in the working directory to a specific state. It is a versatile tool for undoing changes in your working directory or staging area. Example: To undo modifications in your working directory and revert a file back to how it was in the last commit, use git restore [file-name]

  4. git clean - used to remove untracked files from the working directory. git clean -n (perform a dry run to show which files would be deleted), git clean -f (force the removal of untracked files).

Advance Manipulation

  1. git rebase - is a Git command used to move or combine a sequence of commits onto a new base in your project. Imagine you started a feature branch a week ago, and since then, other team members have updated the main branch. Your branch is now behind, and before you finish your feature, you need to update it with the latest changes from the main branch. Using rebase, you can take all the changes you made on your feature branch and re-apply them on top of the current end of the main branch. This makes it look as if you started your work on the latest version of the main branch. Switch to the feature branch git checkout feature and rebase the feature branch onto main git rebase main

  2. git stash - used to temporarily save changes that you're not ready to commit. It takes both staged and unstaged modifications and saves them away, giving you a clean working directory. Purpose: Stash is useful when you need to quickly switch contexts and work on something else, but you're not ready to commit the work in progress on your current branch. Usage: You would use git stash when you need to clear away your current changes but plan to come back to them later. For example, if you need to fix a bug on another branch but don't want to commit your current work yet. git stash pop is used to apply the changes stored in a stash back onto your working directory and then immediately remove the stash entry from your stash list

Summary

In this blog post, we've explored the fundamental concepts and commands of Git, a critical tool for any developer aiming to effectively manage and collaborate on software projects. From basic file handling with statuses such as untracked, modified, and staged, to advanced operations like branching, merging, and rebasing, we've covered how Git can streamline your development process.

By integrating these Git practices, developers can enhance project transparency, increase efficiency, and reduce errors, making their workflow smoother and more productive.

If you liked this post, please give it a like and share it with others who might find it helpful!

0
Subscribe to my newsletter

Read articles from Artur directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Artur
Artur