Useful Git commands — Part #1
I decided to compile a list of git commands that I found useful (and lacked extensive explanation) while working on my first couple of Git projects. As a beginner, I’ve tried to simplify the use case for these commands. Here’s hoping you find them useful, as well!
1. git config --global user.name "xyz"
git config --global user.mail "xyz@xyzmail.com"
To set a username/email globally for the Git project. Typically when you execute git config, git searches for the configuration in /etc/gitconfig. Using the --global attribute, we can ensure the above values are picked up from (and stored in) .git/config.
2. git config --system user.name "xyz"
git config --system user.mail "xyz@xyzmail.com"
If you pass the option --system to git config, it reads and writes from the file /etc/gitconfig specifically. This is a system-wide repository as opposed to the local repository configuration in .git/config.
3. git add "file_name"
Adding a file explicitly before committing so as to ensure that the changes in the staged file are being tracked.
4. git commit -m "message"
Committing with a message specified in the “message” string
5. git commit --amend -m "message"
This amends the message of the previous commit without having to go through the vi/vim editor.
6. git push origin <branch_name> --force
Forcefully pushes the contents of current directory to branch_name
7. git rebase <branch1> <branch2>
This command rebases branch 1 on branch 2 so that all commits from branch 2 will be included in branch 1.
However it is more common to first checkout branch1 and then rebase it.
git checkout branch1
git rebase branch 2
8. git checkout branch1
git merge branch2
The two statements together merge contents of branch1 to branch2.
9. git commit --signoff --message “message_content”
As an alternative to #1, if your username is specified in .git/config you can use the above command to signoff with a message.
That’s all for part #1 of this series, folks! Get notified immediately when the next in this series gets published by following me here or on Twitter: @Divya_Mohan02.