We’ll look at inspecting, removing, and tidying with Git. We’ll also cover ways to escape Vim and save time with Bash aliases and Git editor configuration.
Here are 10 commands to know and some of their common flags. Each command links to the Atlassian Bitbucket guide for that command.
Let’s look at inspecting changes first.
git diff
— See all file changes locally. A file name can be appended to show changes for only one file. git log
— See all commit history. Can also be used for a file with git log -p my_file
. Enter q
to exit. git blame my_file
— See who changed what and when in my_file. git reflog
— Show a log of changes to the local repository’s HEAD. Good for finding lost work. Inspecting things with Git isn’t terribly confusing. In contrast, Git provides a plethora of options for removing and undoing commits and file changes.
git reset
, git checkout
, and git revert
are used to undo the effects of changes to your repository. These commands can be tricky to keep straight.
git reset
and git checkout
can be used on both commits and individual files.git revert
is used only at the commit level.
If you are just dealing with your own local commits that haven’t been merged into collaborative remote work, you can use any of these commands.
If you are working collaboratively and need to neutralize a commit in the remote branch, git revert
is your tool.
Each of these commands can take a variety of options. Here are common uses:
git reset --hard HEAD
— Discard staged and unstaged changes since the most recent commit.Specify a different commit instead of HEAD
to discard changes since that commit. --hard
specifies that both the staged and unstaged changes are discarded.
Make sure you don’t discard a commit from a remote branch that your collaborators are depending upon!
git checkout my_commit
— Discard unstaged changes since my_commit.HEAD
is often used for my_commit
to discard changes to your local working directory since the most recent commit.
checkout
is best used for local-only undos. It doesn’t mess up the commit history from a remote branch that your collaborators are depending upon!
If you use checkout
with a branch instead of a commit, HEAD
is switched to the specified branch and the working directory is updated to match. This is the more common use of the checkout
command.
git revert my_commit
—Undo the effects of changes in my_commit. revert
makes a new commit when it undoes the changes.revert
is safe for collaborative projects because it doesn’t overwrite history that other users’ branches might depend upon.
revert
is safe
Sometimes you just want to delete untracked files in your local directory. For example, maybe you ran some code that created lots of different types of files that you don’t want in your repo. Oops. 😏 You can clean them in a flash!
git clean -n
—Delete untracked files in the local working directory.The -n
flag is for a dry run where nothing is deleted.
Use the -f
flag to actually remove the files.
Use the -d
flag to remove untracked directories.
By default files untracked by .gitignore will not be deleted, but this behavior can be altered.
Now that you know the tools for undoing things in Git, let’s look at two more commands to keep things orderly.
git commit --amend
— Add your staged changes to the most recent commit.If nothing is staged, this command just allows you to edit the most recent commit message. Only use this command if the commit has not been integrated into the remote master branch!
git push my_remote --tags
—Send all local tags to the remote repo. Good for versioning changes.If you’re using Python and making changes to a package you built, bump2version will automatically create tags for you. Once you’ve pushed your tags, you can use them in your releases.
With Git, you might occasionally find yourself thrown into a Vim editor session. For example, say you tried to commit without a commit message — Vim will open automatically. If you don’t know Vim, this kind of stinks — see 4,000+ up votes on this Stack Overflow answer for how to get out of it.
Freedom!
Here’s your four-step plan to escape Vim with a saved file:
i
to enter insert mode.Esc
.:x
. Don’t forget the colon.Voilà, you’re free! 😄
To avoid Vim altogether, you can change your default editor in Git. Here are docs with the commands for common editors. Here’s the command to change the default for the editor I use, Atom:
git config --global core.editor "atom --wait"
Assuming you have Atom installed, you can now resolve your Git issues inside it. Yeah!
Add shortcuts for Git commands by adding the following aliases to your .bash_profile.alias gs='git status '
alias ga='git add 'alias gaa='git add -A ’
alias gb='git branch ’
alias gc='git commit ’
alias gcm='git commit -m ’
alias go='git checkout ’
You can adjust to make shortcuts for whatever Git commands you like.
If you don’t have a .bash_profile, you can make one on macOS with the following:
touch ~/.bash_profile
and then open it with:
open ~/.bash_profile
See more info on .bash_profile here.
Now when you type gs
in your terminal it’s the same as typing git status
. Note that you can enter other flags in your terminal after your shortcut.
You also can make Git aliases, but those require you to type git
before your shortcut command. Who needs those extra key strokes?
In this article you have seen a bunch of key Git commands and configured your environment to save you time. Now you have a foundation in Git and GitHub. Ready for next steps?
I hope you found this intro to Git and GitHub useful. If you did, please share it on your favorite social media channels so others can find it too.
Thanks for reading ❤
If you liked this post, share it with all of your programming buddies!
Follow us on Facebook | Twitter
☞ How To Use Git: A Reference Guide
☞ Setting up Gridsome with GitLab, NetlifyCMS and Netlify
☞ Basic Git/GitHub Cheat Sheet
☞ Getting Started Faster with Git and GitHub
☞ Top Popular Python open-source projects on GitHub
☞ Build Docker Images and Host a Docker Image Repository with GitLab
☞ 5 GitHub Repositories that Every New Developer Must Follow
#git #github #web-development