![](images/colors.jpg)
![](../images/bg1.jpg)
Git Command - best cheat sheet
![](profiles/bk20240114180124bDigTYx4z6QBZEINpyP.png)
Git is widely recognized as the leading distributed version control system across organizations today, and for good reason. The features and benefits Git brings to development workflows are invaluable, enhancing productivity and significantly saving time.
Git Repositories Structure
Before diving into essential Git commands, it’s important to understand the underlying structure of Git as a distributed system. Git creates different types of repositories, each serving a distinct purpose in the workflow and enabling efficient collaboration. This structure forms the backbone of Git’s version control capabilities, making it an invaluable tool in modern development environments.
Remote Repository: This is a centralized version of the repository hosted on a server, enabling collaboration among multiple contributors. It serves as the authoritative source of truth, where updates are pushed to or pulled from by team members working on the project.
Eg. When we create repository on github server. Its a centralized repository and consider as remote repository because anyone can view, copy and perform various action worldwide.
Local Repository: This is the developer’s local copy of the repository, stored on their machine. It includes the full project history and allows for commits and modifications that are isolated from the remote repository until they are pushed. This local copy empowers developers to work offline and experiment freely.
Note: It’s important to clarify that the project repository in an IDE like IntelliJ, Eclipse, or any other environment is not the same as Git's local repository. While an IDE project repository is often visible and specific to that IDE’s workspace, the local repository in Git is stored in a hidden .git
directory on your local machine. This local repository maintains the entire history of the project and allows for commits across different workspaces or IDEs on the same machine.
This means that you can work in multiple workspaces, or switch between different IDEs on the same machine, while Git’s local repository consistently tracks and saves all committed changes, providing a unified and efficient way to manage your project’s versions.
Staging Area (Stage Repository): The staging area is an intermediate zone where changes are added before committing them to the local repository. It allows developers to group and review specific changes for a clear, organized commit history, ensuring only finalized code is committed.
Working Directory (Working Repository): The working directory (or working repository) is where developers actively create and edit files. This directory reflects the current state of the project and serves as the primary environment for ongoing development. Not all files in the working directory need to be tracked by Git, however. Only the files added to tracking will move to the staging area for inclusion in the next commit.
If a file remains untracked, it will not be added to the staging area, and therefore, it won’t be part of Git’s version history. This selective tracking provides flexibility, allowing developers to exclude temporary, personal, or development-specific files from version control until they’re ready for inclusion. This approach keeps the repository clean and focused on meaningful changes.
Git Important Commands
Let’s take a closer look at the foundational commands that allow developers to leverage the full power of Git.
git clone
git clone we use when we need to copy remote repository to working repository (usually our local machine) "We can do specific folder, specific tag and specific commit clone (called shallow clone) too "
git add
git add, we use when we need to index our files in working area. Well until we will not index our files, it will be non-tracked files. so to make file from non-tracked to tracked, we need to add file in index, which is usually move to staging area.
"We can do specific folder, specific file and all files in all directory too"
git reset (opposite to add)
If any one our file is tracked mode , and we want bring back that file into non-tracked mode, then we use git reset
"usually we can reset file and commit both"
git commit
git commit migrate file from staging area to local repository. with git commit we always have to provide message. reverting the commit we can do through reset too.
"usually after git commit, our current branch move 1 commit ahead from remote branch"
git push
git push migrate file from local repository to remote repository. after git push command all committed files will be visible in remote repository
git fetch
git fetch bring copy from remote repository to local repository. (git doesn't update these file in working repository in git fetch)
git merge
git merge bring updated files from local repository to working repository
git pull
git fetch + git merge