In the previous blog Git Basics: A Simple Guide for Beginners Part 1 we got familiar with the basics of Git, let's explore some intermediate Git commands that can help to work more efficiently and effectively.
git status
The git status
command is used to display the current state of your Git repository. It shows which files have been modified, added, or deleted since the last commit, and which files are currently staged for the next commit. Running git status
is a good way to check the status of your repository before committing your changes.
git log
The git log
command shows a chronological history of all the commits made to your repository. It displays the commit hash, author, date, and commit message for each commit. You can use various options with git log
to filter and format the output in different ways.
git reset
The git reset
command allows you to unstage changes and move the HEAD pointer to a previous commit. It can be used to undo a commit, unstage files, or reset your repository to a previous state. There are different options for git reset
, including --soft
, --mixed
, and --hard
, which determine how the reset is performed.
git revert
The git revert
command is used to undo a commit by creating a new commit that undoes the changes made in the previous commit. This is useful when you want to undo a commit without losing its history. git revert
can be used to undo multiple commits in sequence.
git branch -d
The git branch -d
command is used to delete a branch in your repository. It can only be used if the branch has been merged into another branch. If you want to delete a branch that has not been merged, you can use git branch -D
instead.
git stash
The git stash
command is used to temporarily save changes that are not yet ready to be committed. This is useful when you need to switch to another branch or work on a different feature, but don't want to commit your changes yet. You can use git stash apply
to apply the changes later.
git tag
The git tag
command is used to create, list, and delete tags in your repository. Tags are useful for marking specific points in your Git history, such as releases or milestones. You can use git tag -a
to create an annotated tag with a message, or git tag -l
to list all the tags in your repository.
git remote
The git remote
command is used to manage remote repositories in your Git project. It allows you to add, remove, and list remote repositories, as well as specify different remote repositories for fetching and pushing changes. You can use git remote add
to add a new remote repository, and git remote -v
to list all the remote repositories.
Real world scenario:
lets explore a scenario where the above learnt commands can come in handy:
Imagine you're working on a team project with several other developers. Each developer has their own feature branch where they're working on a specific feature of the project. Once their feature is complete, they need to merge their branch into the main branch, which will trigger a new build and deploy the updated code to a staging environment for testing.
However, one of the developers, let's call him Ali, has been working on his feature branch for a while and hasn't kept it up-to-date with the main branch. Meanwhile, other developers have been making changes to the main branch, which means that Ali's branch is now outdated and conflicts may arise when merging.
To resolve this issue, Ali needs to fetch the changes from the main branch and merge them into his feature branch. Here's how he can do that using Git intermediate commands:
First, Ali needs to switch to his feature branch using the following command:
git checkout feature-branch
Next, he needs to fetch the latest changes from the main branch using the following command:
git fetch origin main
Once the changes are fetched, Ali can merge the main branch into his feature branch using the following command:
git merge main
Git may detect conflicts between the changes made to Ali's feature branch and the changes made to the main branch. To resolve these conflicts, Ali needs to manually edit the affected files and then commit the changes using the following commands:
git add . git commit -m "Resolved merge conflict with main branch"
Finally, Ali can push his changes to the remote repository using the following command:
git push origin feature-branch
By using these intermediate Git commands, Ali was able to merge his feature branch into the main branch without causing conflicts and delaying the project's progress.
Conclusion
These intermediate Git commands can help you to work more efficiently and effectively with Git. By mastering these commands, you'll be able to handle more complex workflows and take full advantage of the power of Git.
Top comments (0)