Mastering Git: Top 18 Git Commands Every Developer Should Know!🚀
🌟 Just landed your first tech job and feeling overwhelmed by all the talk about Git? 🤔 You’re not alone. Let’s demystify this essential tool and get you up to speed with the commands you need to know! 💻✨
Git is your go-to version control system, perfect for tracking every change in your code. Think of it as a time machine for developers! ⏳ Meanwhile, GitHub is the ultimate platform for hosting and sharing your projects with the world. 🌍
1.git clone
What It Is: git clone is the command you use to create a copy of an existing Git repository. This command allows you to download the entire repository, including its history, branches, and files, onto your local machine.
How It Works: When you execute git clone <repository-url>, Git copies all the files and the version history from the specified remote repository to your local system. It also automatically sets up the remote connection, allowing you to easily fetch and push changes later on.
When To Use git clone: Starting from an Existing Repository: Use git clone when you want to work on a project that already exists in a remote repository. This command gives you a complete working copy of that project on your local machine.
_________________________________________________________________
2. git init
What It Is: git init is the command you use to create a new Git repository. Think of it as the first step in your version control journey. By running this command, you’re telling Git, “This folder is now a Git repository!”
How It Works: When you execute git init in your project directory, Git creates a hidden folder called .git. This folder contains all the necessary files and data that Git uses to track your project’s changes.
When To Use Git init:
1.Starting a New Project: Use git init when you create a new project, like a website or application. This command turns your project folder into a Git repository.
Example: Create a new folder for your coding project, open your terminal, navigate to that folder, and run git init to start tracking changes.
2. Turning an Existing Project into a Git Repository: If you have an existing project and want to start tracking it with Git, navigate to the project’s folder and run git init.
Example: If you’ve been working on a project without version control, run git init in that folder to begin tracking your changes.
Note : you don’t need to run git init when you clone a repository. When you use the git clone command, Git automatically initializes a new repository in the specified directory and copies all the data from the remote repository into it. So, just running git clone <repository-url> is enough to set everything up.
_________________________________________________________________
3. git add
What It Is: git add is the command used to stage changes in your Git repository. Staging allows you to prepare specific changes for the next commit, making it a crucial step in the version control process.
How It Works: When you execute git add <file> (or git add . to stage all changes), Git takes the current state of the specified files and adds them to the staging area. This tells Git that you want to include these changes in your next commit.
When To Use git add: Staging Changes for a Commit: Use git add to specify which modified or new files you want to include in your next commit. This allows you to control what changes get recorded in your project history.
Note: Always remember that changes must be staged with git add before they can be committed with git commit.
_________________________________________________________________
4. git commit
What It Is: git commit is the command used to save your changes to the local repository. It creates a new commit, which is a snapshot of your current project state, allowing you to track the history of changes over time.
How It Works: When you run git commit -m “Your commit message here”, Git takes all the changes that you have staged and creates a new commit with a unique identifier (commit hash). Each commit includes metadata such as the author, date, and a commit message describing the changes made.
When To Use git commit: Saving Changes: Use this command when you have made changes to your files and want to save them in the repository’s history.
Note: If you run git commit without the -m option, Git will open your default text editor to allow you to write a more detailed commit message.
_________________________________________________________________
5. git status
What It Is: git status is a command that provides a summary of the current state of your Git repository. It shows which changes have been staged, which files are modified but not yet staged, and which files are untracked. In Git, “staged” refers to the state of files that have been marked to be included in the next commit.
How It Works: When you execute git status, Git analyzes the state of your working directory and staging area. It compares your files to the last commit to determine what changes are present and displays this information in a clear format.
When To Use git status:
- Checking the Current State: Use git status to get an overview of your repository before you make a commit. It helps you see which files have been modified, added, or deleted.
- Ensuring Your Work is Saved: Before switching branches or pulling new changes, it’s good practice to run git status to ensure you don’t have any unsaved work or conflicts.
_________________________________________________________________
6. git restore
What It Is: git restore is a command used to restore files in your working directory to a previous state. This command allows you to discard changes made to files, either by resetting them to the last committed version or by restoring them from a specific commit.
How It Works: When you run git restore, Git updates the specified files in your working directory with the content from the index (staging area) or a previous commit. This helps you revert changes or recover files without affecting the commit history.
When To Use git restore:
- Discarding Uncommitted Changes: Use this command to revert changes made to files in your working directory that you haven’t yet staged or committed. Example: git restore <file-name>
- Restoring Staged Changes: If you’ve staged changes (added them to the index) but want to unstage them and restore the file to its last committed state, you can use git restore — staged <file-name>
- Recovering a File from a Specific Commit: If you need to restore a file from a previous commit rather than the latest one, you can specify the commit hash. Example: git restore — source=<commit-hash> <file-name>
Note:
- Be cautious when using git restore, as it will permanently discard uncommitted changes in the specified files.
- If you want to restore all files in the working directory, you can use git restore . (with caution).
_________________________________________________________________
7. git log
What It Is: git log is the command used to view the commit history of your Git repository. It provides a detailed list of commits, including information such as the commit hash, author, date, and commit message.
How It Works: When you run git log, Git displays a chronological list of commits made in the current branch. By default, it shows the most recent commits at the top, giving you an overview of the project’s development history.
When To Use git log:
- Reviewing Commit History: Use this command to check the history of changes in your repository, helping you understand the progression of the project and the contributions made over time.
- Identifying Specific Commits: If you need to find a particular commit (for example, to revert or inspect changes), git log allows you to locate commits based on messages, authors, or dates.
Note:
- You can use various options with git log to customize the output, such as displaying a specific number of commits, formatting the output, or filtering by dates.
- To navigate through the log, use the arrow keys, and press q to exit the log view.
_________________________________________________________________
8. git revert
What It Is: git revert is a command used to create a new commit that undoes the changes made by a previous commit. This is a safe way to remove changes from your project history without rewriting commit history, which helps maintain the integrity of your repository.
How It Works: When you run git revert <commit-hash>, Git applies the inverse of the specified commit’s changes to your working directory and then creates a new commit with these changes. This allows you to effectively “undo” changes while preserving the history of the repository.
When To Use git revert: Undoing a Specific Commit: Use this command when you want to revert changes introduced by a specific commit without affecting subsequent commits. Example: git revert abc123
Note:
- If the commit being reverted has conflicts with subsequent changes, Git will notify you to resolve those conflicts before completing the revert.
- You can revert multiple commits by providing a range, but it’s often best to do this one at a time to avoid complex conflicts.
_________________________________________________________________
9. git stash
What It Is: git stash is the command used to temporarily save changes in your working directory that you are not yet ready to commit. This allows you to switch branches or work on other tasks without losing your current progress.
How It Works: When you run git stash, Git takes the changes you’ve made to your tracked files (including staged changes) and stores them in a stack called the stash. Your working directory is then reverted to the last commit, allowing you to start fresh without affecting your ongoing work.
When To Use git stash: Temporarily Saving Changes: Use this command when you want to switch to another branch or perform other tasks without committing your current changes.
_________________________________________________________________
10. git stash pop
What It Is: git stash pop is the command used to reapply the changes you previously stashed and simultaneously remove the most recent stash entry from the stash list. It’s a convenient way to restore your stashed work while cleaning up your stash.
How It Works: When you run git stash pop, Git takes the changes stored in the most recent stash entry and applies them to your current working directory. After applying the changes, it removes that stash entry from the list, effectively “popping” it off the stash stack.
When To Use git stash pop: Continuing Work After Stashing: After completing other tasks or switching branches, use git stash pop to bring back your stashed changes into your current working directory, allowing you to continue where you left off.
Recommended by LinkedIn
Note:
- If there are conflicts while applying the stashed changes, Git will indicate which files need resolution. You will need to resolve those conflicts before you can continue working.
- If you want to apply the stash without removing it from the stash list, use git stash apply instead.
_________________________________________________________________
11. git stash clear
What It Is: git stash clear is the command used to remove all stashed changes from your Git repository. It permanently deletes all entries in your stash, freeing up space and ensuring that you are not storing any uncommitted changes.
How It Works: When you run git stash clear, Git removes all stash entries stored in the stash list. This action is irreversible; once the stashes are cleared, you cannot recover them.
When To Use git stash clear:
- Cleaning Up Your Stash: Use this command when you have stashed multiple changes that you no longer need. It helps maintain a clean working environment by removing unnecessary stashes.
- Finalizing Work: After you’ve finished a series of stashing and applying changes, and you want to clean out old stashes before moving on, you can use this command.
Note:
- Before using git stash clear, you might want to review your stashes with git stash list to ensure that you really want to delete all of them.
- If you only want to remove the most recent stash, consider using git stash drop instead.
_________________________________________________________________
12. git remote add origin
What It Is: git remote add origin is the command used to add a new remote repository to your local Git repository. The term “origin” is the conventional name for the primary remote repository, where you typically push your changes.
How It Works: When you execute git remote add origin <repository-url>, Git creates a new remote entry with the name “origin” and associates it with the specified URL. This allows you to easily reference the remote repository in future Git commands, such as git push or git fetch.
When To Use git remote add origin:
- Setting Up a New Repository: Use this command when you have created a new local repository and want to connect it to a remote repository, such as one hosted on GitHub, GitLab, or Bitbucket.
- Reconnecting to a Remote Repository: If you have previously removed or changed your remote repository and want to re-establish the connection, you can use this command to add the remote again.
_________________________________________________________________
13. git remote -v
What It Is: git remote -v is a command used to display the list of remote repositories associated with your local Git repository, along with their URLs. This is useful for verifying where your changes will be pushed or where you will fetch updates from.
How It Works: When you execute git remote -v, Git retrieves the information about the remotes configured for the repository. The output will show both the fetch and push URLs for each remote, allowing you to see how your local repository interacts with remote repositories.
When To Use git remote -v: Checking Remote Repositories: Use git remote -v to quickly check which remote repositories are linked to your local repository and their corresponding URLs.
_________________________________________________________________
14. git pull
What It Is: git pull is the command used to fetch and integrate changes from a remote repository into your current branch.
How It Works: When you execute git pull, Git performs two actions:
- It first runs git fetch, which downloads the latest changes from the remote repository but doesn’t integrate them into your local branch.
- It then runs git merge (or git rebase, depending on your configuration), which combines the changes from the remote branch into your current branch.
Example: git pull origin main
Breakdown:
- git pull: The command to fetch and merge changes.
- origin: The name of the remote repository (default name for the original repository you cloned from).
- main: The name of the branch you want to pull updates from (replace with your branch name if different).
When To Use git pull: Updating Your Local Repository: Use git pull to ensure your local branch is up to date with the remote branch before starting new work. This helps avoid conflicts and ensures you’re building on the latest changes.
Note:
When using git pull, be aware that it can sometimes lead to merge conflicts if there are changes in the same lines of code. If this happens, Git will prompt you to resolve the conflicts before completing the merge. To avoid potential issues, it’s often a good practice to commit or stash your changes before pulling.
_________________________________________________________________
15. git push
What It Is: git push is the command used to upload your local repository changes to a remote repository. It is essential for sharing your work with others and updating the central repository with your commits.
How It Works: When you run git push, Git sends your committed changes from your local branch to the corresponding branch in the remote repository. This process updates the remote branch with your latest commits, making them available to other collaborators.
When To Use git push:
1. Sharing Your Changes: Use git push to upload your local commits to the remote repository after you’ve completed work on a feature or bug fix. Example: git push origin main
This command pushes your commits from your local main branch to the main branch on the remote repository named origin.
2. Pushing to a New Branch: If you create a new branch locally and want to push it to the remote repository, you can specify the branch name. Example:`git push -u origin feature-xyz`
The -u option sets the upstream tracking reference, so future pushes can be done with just git push.
3. Updating Remote Branches: After making changes and commits in a feature branch, you can push updates to the corresponding branch in the remote repository. Example: git push origin feature-xyz
Note:
Be aware that if someone else has pushed changes to the remote branch since your last pull, you may need to first pull those changes into your local branch and resolve any conflicts before you can successfully push your changes.
_________________________________________________________________
16. git branch
What It Is: git branch is the command used to manage branches in a Git repository. It allows you to create, list, and delete branches, which are essential for organizing work on different features or versions of a project.
How It Works: When you run git branch, Git interacts with the repository’s branch structure. A branch in Git is essentially a pointer to a specific commit, allowing you to diverge from the main line of development and work independently on features, fixes, or experiments.
When To Use git branch:
- Listing Existing Branches: Use git branch without any arguments to see a list of all branches in your repository. The current branch will be highlighted with an asterisk. Example: git branch
- Creating a New Branch: Use git branch <branch-name> to create a new branch without switching to it. This is useful for setting up a branch for a new feature or fix. Example: git branch feature-xyz
- Deleting a Branch: Use git branch -d <branch-name> to delete a branch that is no longer needed. This can only be done if the branch has been fully merged; otherwise, you can use -D to force deletion. Example: git branch -d feature-xyz
- Renaming a Branch: You can rename the current branch using git branch -m <new-branch-name>. Example: git branch -m new-feature
_________________________________________________________________
17. git checkout
What It Is: git checkout is the command used to switch between branches or restore working tree files in a Git repository. It allows you to navigate your project’s history and work on different branches easily.
How It Works: When you execute git checkout, Git updates the files in your working directory to match the version stored in the specified branch or commit. This command modifies your HEAD pointer to point to the chosen branch or commit, effectively changing the current working state of your repository.
When To Use git checkout:
- Switching Between Branches: Use git checkout to switch from your current branch to another branch in your repository.
- Creating a New Branch: You can also create a new branch and switch to it in one command by using the -b option. Example: To create a new branch called new-feature and switch to it, use: git checkout -b new-feature
- Restoring Files: If you want to restore a specific file to its state in a different branch or commit, you can specify the file after the branch or commit reference. Example: To restore a file named example.txt from the main branch, use: git checkout main — example.txt
_________________________________________________________________
18. git merge
What It Is: git merge is the command used to combine changes from one branch into another.
How It Works: When you execute git merge, Git takes the changes from the specified branch and integrates them into your current branch. The process involves:
- Fast-Forward Merge: If the current branch has not diverged from the branch being merged, Git simply moves the pointer forward to the latest commit.
- Three-Way Merge: If the branches have diverged, Git creates a new commit that combines the changes, using the latest commits from both branches as reference points.
When To Use git merge:
- Integrating Changes from a Feature Branch: Use git merge when you want to bring changes from a feature branch (e.g., feature-branch) into your main branch (e.g., main).
- Combining Updates from a Remote Branch: After pulling updates from a remote branch, you might want to merge those changes into your local branch.
Note:
Before merging, ensure your current branch is up to date to minimize conflicts. If conflicts arise during the merge, Git will indicate which files need resolution. After resolving conflicts, stage the changes and commit to complete the merge.
_________________________________________________________________
And there you have it! Master these commands to level up your coding game! Happy coding! 🎉💻