Advanced GIT Commands
Work with some advanced git commands!
This article will help you to understand some advanced GIT commands and it’s usages.
Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency.
Some General Commands
-default editor
Set the VS Code as default editor:
git config --global core.editor code
View the config file in the editor:
git config --global -e
See the content inside the global config file without opening the file:
git config --global --list
-logs
See the logs with complete details:
git log
See the logs in a much cleaner way:
git log --oneline
See the logs with the graph:
git log --graph
Apply different type of filters on logs:
git log --oneline @{1.day.ago}
git log --oneline -3
git log --author="John"
Amend
git amend
commands can rewrite the history by updating the previous commits details like the message, adding or removing a file, changing a part of the code.
git commit --amend -m "updated message here"
The above command will change the commit message of the most recent commit. Also, it will take the present changes from the stage area if any, and add them to this new amend commit.
All the amend original commits are called unreachable and will not be a part of any branch history.
Reflog
git reflog
commands can be used to see the unreachable commits. The normal git log
will not display the unreachable commits.
git reflog
It is possible to point the head to a specific unreachable commit then we can create a new branch out of it to start reworking.
reflog
can also be used to clean unreachable commits.
git reflog expire --expire-unreachable=now --all
The above command creates a variable for the Garbage Collector for the pruning operation.
To run the GC:
git gc --prune=now
Squash & Merge
git squash
command will take multiple commits and combined them into a single commit.
git checkout main
git merge --squash bugfix
Consider, if we have a series of commits in bugfix
branch the above commands will squash all those commits into a single commit and will be merged into branch main
with a single new commit. This way we can concisely keep the history.
Squash & Merge can also be done directly in GitHub.
After raising the PR in GitHub, expand the `Merge pull request` button then select the `Squash and merge` option.
Reset
git reset
command will be used to unstage the changes from the staging area or allow going back to a particular commit and able to rework that specific commit.
There are 2 types of reset
available:
-soft
Unstage all the files from the staged area:
git reset head
Unstage all the files from a specific commit:
git reset 09c02f0
-hard
Completely wipe out the changes from the staging area:
git reset head --hard
Completely wipe out the changes from a specific commit:
git reset 09c02f0 --hard
Revert
git revert
command will help to undo a specific commit we made.
git revert 09c02f0
Rebase
git rebase
will result in a linear commit history, it looks like we never branch.
# Create a feature branch based on master
git checkout -b featureA
# Edit files and do some commits
git commit -am "First"
git commit -am "Second"
# Switch to the master to add some changes
git checkout master
# Edit files and do some commits
git commit -am "Third"
git commit -am "Fourth"
# Now the master branch is 2 commits ahead
# Switch to feature branch for the rebase to get a linear commit history
git checkout featureA
git rebase master
Cherry-pick
git cherry-pick
enables arbitrary git commits to be picked by reference and appended to the current working HEAD.
git cherry-pick 09c02f0
Tagging
git tag
is a nice way to put a bookmark on the commits that are easy to find and also used to publish the information about the tag.
It’s common practice to prefix the version names with the letter v
. Some good tag names might be v1.0
or v2.3.4
.
List all the available tags:
git tag
There are 2 types of tag
available:
-Lightweight
git tag v1.0.0
-Annotated
This tag
will have a few more additional information than the Lightweight tag.
git tag -a v1.0.1
Some additional commands related to tags
Display all the tags that are part of version 1:
git tag -l "v1*"
tag
a specific commit in the past:
git tag v0.0.0 09c02f0
Delete a tag:
git tag -d v0.0.0
push
the tags to the remote repo:
git push --tags
The command that automatically push/ pull the tags, don’t need to do separately:
git config --global push.followTags true
Open for contributions: GitHub Repo
Good luck!