Git is a powerful version control tool that plays a crucial role in modern software development. Familiarizing yourself with essential Git commands is fundamental to effectively manage your source code and collaborate with other developers. In this guide, we will explore all the Git commands you need to know to become an expert.
The first step in versioning your project with Git. This command initializes a new Git repository in your directory.
Use this command to create a local copy of an existing Git repository.
git clone <repository_URL>
Add changes to your upcoming commit. You can specify individual files or use
git add . to add all modified files.
git add <file_name>
Record changes in your repository with a descriptive message.
git commit -m "Commit message"
Update your local repository with changes from the remote repository.
Send your local changes to the remote repository.
List all branches in your repository and show the currently checked-out branch.
Switch between branches or create a new branch.
git checkout <branch_name>
Combine changes from one branch into another.
git merge <branch_name>
View the commit history of the repository.
```bash git log ```
Temporarily stash uncommitted changes.
```bash git stash ```
Undo changes in the repository.
```bash git reset <commit_hash> ```
List configured remote repositories.
```bash git remote -v ```
Download information from the remote repository but do not automatically merge.
```bash git fetch ```
Rearrange commits for a cleaner timeline.
```bash git rebase <branch_name> ```
Mark commits for specific versions of your project.
```bash git tag <tag_name> ```
Check the current state of your repository, including modified and untracked files.
```shell git status ```
Display the differences between files in your working directory and the staging area.
```shell git diff ```
Add a new remote repository to your Git configuration.
```shell git remote add <remote_name> <remote_URL> ```
Remove a remote repository from your Git configuration.
```shell git remote remove <remote_name> ```
Update remote repository information and download changes but do not automatically merge.
```shell git fetch ```
Perform an interactive rebase to rearrange, edit, or merge commits.
```shell git rebase -i <commit_hash> ```
Apply a specific commit from one branch to another.
```shell git cherry-pick <commit_hash> ```
Visualize the commit history graphically, showing branches.
```shell git log --graph ```
Remove untracked files from the working directory.
```shell git clean -n # Show files to be removed (dry run mode) git clean -f # Remove untracked files (with caution!) ```
Manage Git submodules within your main repository.
```shell git submodule add <submodule_URL> <local_path> ```
This guide provides a comprehensive overview of Git commands that are essential for any developer. As you advance in your Git journey, you can explore more advanced commands and branching strategies. However, with these basic commands, you are ready to start effectively managing your source code.
Remember that consistent practice is the key to mastering Git. So, start experimenting with these commands in your own projects and see how they can enhance your efficiency in software development.