I'm learning a lot about Git on the project I'm working on at the company right now. And after only 2 months I already feel like a Git pro π€.
Git commands I never used before (or needed because I was working alone and actively on only one branch at a time), or never heard of, are now part of my daily use.
I am no longer afraid of Git. In addition, working with the GitLens extension also makes it a little easier with some commands and is another reason I feel much more confident using Git commands now.
Let me show you some Git commands I learned through this project!
git branch
I work with so many different branches that it is impossible to remember all the names. This command displays a list of all local branches. The branch I am currently on is highlighted.
If I want to merge into the master branch from the console, or switch to a specific branch, I can now copy its name and paste it into the command I want to run.
In most cases, however, I use the built-in VS Code function to change the branch by clicking in the lower left corner where the current branch is displayed, and then selecting the desired branch, which changes by itself when I click on it.
git stash
- git stash pop
I often need to switch between branches. That's why git stash
comes in handy. If you make changes in the current branch and then need to switch, but don't want to push those changes (yet), you can cache them.That is, you save them and can access them later (no matter on which branch).
git fetch
It happens from time to time that multiple people are working on same branch. So I use git fetch
to see if there are any changes since my last pull on the remote branch. It prevents my local branch from being overwritten with changes I might not want when I pull.
git revert commit-hash
It may happen that two user stories overlap, and changes negatively affect other parts of the code, and you may want to undo your commits and look for another solution.
Even if it called revert, the revert is just another commit to the history, so it is not really gone, but set your code back to how it was before by creating another commit. You have to insert the hash of the commit you want to revert. To get this specific hash, the next command I am going to describe has to be executed.
git log --pretty --oneline
The git log
command shows a list of all the commits made to a repository. You can see the full hash of each Git commit, the message associated with each commit, and more metadata. This command displays the history of a repository.
By adding --pretty --oneline
the log compresses these information to a single line and shorten the hash, the version I actually need for the git revert
commit mentioned above.
If you want to show only the last commit, add a -1
flag in the middle of the command git log -1 --pretty --oneline
. You can use any number here, and it will show your the last number commits.
git rebase master
When the master branch of our project is updated with all the completed user stories (Definition of Done) from the current week, we also need to update our current branch we are working on to avoid future merge conflicts. So I go to the master branch and pull the latest changes locally, go back to my current branch and use git rebase master
before doing a forced push with git push -f
to add the changes to the current branch.
git rebase --abort
- git merge --abort
Sometimes it happens that when checking out a branch git asks me to first rebase or resolve merge conflicts - changes I don't want to make at this time. So I use one of the above commands to get out of this situation without changing anything.
Thanks for your reading and time. I really appreciate it!
Top comments (0)