Some Necessary Git Commands and Use Cases

Ariful Islam
5 min readJul 10, 2024

--

Git is a popular technology for managing version control and team work. Here below are a lot of git useful commands.

| Command                   | Summary                                      | Details                                                                                        | Use Case                                                                                   |
|---------------------------|----------------------------------------------|------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------|
| git init | Initialize a new Git repository | Creates a new Git repository in the current directory | Starting a new project with version control |
| git clone [url] | Clone a repository | Downloads a repository from a remote URL and sets up local repo with remote linked | Getting a copy of an existing project to work on |
| git status | Check the status of the repository | Displays paths that have differences between the index file and the current HEAD commit | Checking which files are staged, unstaged, and untracked |
| git add [file] | Add files to staging area | Adds specified files to the staging area for the next commit | Preparing changes to be committed to the repository |
| git commit -m "[message]" | Commit changes | Records changes to the repository with a message | Saving staged changes with a description of what was changed |
| git push | Push changes to remote repository | Uploads local repository content to a remote repository | Sharing changes with others by updating the remote repository |
| git pull | Fetch and merge changes from remote repo | Fetches and merges changes from the remote repository into the current branch | Updating local repository with the latest changes from the remote repository |
| git branch | List branches | Lists all branches in the repository and indicates the current branch | Viewing all branches in the repository |
| git checkout [branch] | Switch branches | Switches to the specified branch and updates working directory to match | Moving to a different branch to work on a different part of the project |
| git merge [branch] | Merge a branch into the current branch | Combines the specified branch's history into the current branch | Integrating changes from another branch into the current branch |
| git log | View commit history | Displays a list of all the commits in the repository's history | Reviewing the history of changes made to the project |
| git diff | Show changes between commits, branches, etc. | Displays differences between various commits, branches, or between working directory and index | Inspecting changes made in the code before committing or between different branches |
| git remote -v | View remote URLs | Shows the URLs of the remote repositories associated with the local repository | Checking which remote repositories are linked to your local repository |
| git fetch | Download objects and refs from another repo | Fetches changes from the remote repository without merging them into the current branch | Getting updates from the remote repository without affecting the local working directory |
| git rebase [branch] | Reapply commits on top of another base tip | Moves or combines a sequence of commits to a new base commit | Incorporating changes from one branch to another while maintaining a linear commit history |

Detailed Use Cases

git init

Details: This command sets up a new Git repository in your project directory. It creates a hidden .git directory which stores all necessary repository files.

Use Case: When starting a new project from scratch and you want to use version control to track changes.

git clone [url]

Details: This command copies an existing Git repository from a remote URL (such as GitHub, GitLab) to your local machine.

Use Case: When you need to start working on an existing project that is stored in a remote repository.

git status

Details: This command shows the current state of the working directory and the staging area. It lets you see which changes have been staged, which haven’t, and which files aren’t being tracked by Git.

Use Case: Before committing changes, to ensure that the correct files are staged and ready to be committed.

git add [file]

Details: This command adds a change in the working directory to the staging area. This prepares the file to be included in the next commit.

Use Case: When you have made changes to a file and want to include it in the next commit.

git commit -m "[message]"

Details: This command captures a snapshot of the project’s currently staged changes. You must provide a commit message that describes the changes.

Use Case: After staging your changes, to save a version of the project that you can later revert to if needed.

git push

Details: This command updates the remote repository with commits made locally.

Use Case: After committing changes locally, to share these changes with others by updating the remote repository.

git pull

Details: This command fetches changes from the remote repository and merges them into the current branch.

Use Case: To update your local repository with the latest changes from the remote repository before starting new development.

git branch

Details: This command lists all the branches in your repository and shows the current branch.

Use Case: To see all available branches and to check which branch you are currently working on.

git checkout [branch]

Details: This command switches the current branch to the specified branch and updates the working directory to match the branch.

Use Case: When you want to work on a different branch, such as when starting a new feature or fixing a bug.

git merge [branch]

Details: This command merges the specified branch into the current branch.

Use Case: After completing work on a feature branch, to integrate the changes back into the main branch.

git log

Details: This command shows the commit history for the repository.

Use Case: To review the project’s history and see what changes were made and by whom.

git diff

Details: This command shows the differences between commits, branches, and more.

Use Case: To inspect changes made in the code before committing them or to compare different branches.

git remote -v

Details: This command shows the URLs of the remote repositories associated with the local repository.

Use Case: To verify which remote repositories your local repository is connected to, useful when working with multiple remotes.

git fetch

Details: This command downloads objects and refs from another repository.

Use Case: To get updates from a remote repository without merging them into the current branch.

git rebase [branch]

Details: This command applies commits on top of another base tip.

Use Case: To integrate changes from one branch to another while maintaining a clean, linear commit history.

--

--

Ariful Islam
Ariful Islam

Written by Ariful Islam

Working on PHP, Laravel, CI, CakePHP, API. Open Source contributor, Cloud Expert, Project Consultant, Technical Writer. Owner at https://programmingmindset.com

No responses yet