I’ve been actively using Git for about 2 years now, but never took the time to learn some of the more advanced, albeit common, commands. I relied on this Git cheat sheet and still do to some extent. It is an absolutely phenomenal resource, but I didn’t try out everything on that sheet and definitely didn’t try to venture out from there.
Here are the commands I’ll be talking about today.
- Git stash
- Git reset
- Git clone –single-branch
- Git amend
Git Stash
You use git stash
when you want to temporarily store changes you’ve made to a working branch but need to switch to start working on something else. This is incredibly handy when you are mid way through a feature but need to work on something else without committing your half completed code.
Note: git stash
won’t stash changes made to untracked or ignored files. If you have this problem, simply use git stash –u
to include the stashing of untracked files.
After you’ve completed your task on the other branch and want to come back to the first branch, just use git stash pop
to reapply the previously stashed changes.
Git reset
At its core, git reset
is used for undoing changes. git reset
has three basic options: --soft, --mixed, and --hard.
Here is a good graphic from Atlassian depicting the three options.
I won’t go into the three different trees in git that correspond with the above icons as it is a little bit out of the scope of this post.
--hard
This is the most destructive and dangerous of the three options. Any previously pending changes in your git repo get reset to match the state of the Commit Tree. This means any pending work that wasn't committed will be lost.
--mixed
This is the default (git reset --mixed
does the same thing as git reset
). It takes things from the Staging Index (resets them) and puts them in the Working Directory. Compare this to --hard where the Staging Index and the Working directory are both reset.
The key take away is that if you want to unstage your pending work and not lose it --mixed is the way to go.
--soft
It only resets the commit history to whatever you specify. --soft basically doesn’t touch the staging index so those changes follow us “back in time.”
Git clone --single-branch
This command allows you to clone a single branch from a remote repo. It’s super helpful if you don’t want to clone the whole repo from the remote. The full command would look like the example below (first one is an example and second would be a real implementation if you wanted to clone the cart branch from my drumverb repo on github).
git clone --single-branch -b [branchname] [host:/dir.git]
git clone --single-branch -b cart git@github.com:NolanHughes/drumverb.git
Git amend
This is a real fun one! I even used it recently on a project. This is for when you just made a slight change to a file and right before that you made a commit. You feel like your small change doesn’t deserve it’s own commit and would be better suited to go in the previous commit.
To do this, just do the following steps:
git add [file_that_was_changed]
git commit --amend --no-edit
That’s it! Your changes that fit in better with the previous commit were added to that commit. If you want to edit the commit message just leave off the --no-edit flag.