Switch branches/tags
Nothing to show
Find file History
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


1 8 Useful But Not Well-Known Git Concepts

For advanced Git usage, I usually leverage GUI of GitHub or BitBucket. But GUI way may not solve some requirements beautifully.

I found several Git fundamental concepts. They are quite useful, but may not be well-known.

Check it out. And let me know what you think!


Q: What is .gitkeep file?

You may know .gitignore very well. But what .gitkeep is?

By default, Git doesn’t track empty directory. A file must exists within it. We can use .gitkeep file to add empty directories into git repo.

Q: What’s git cherry-pick?

Pick changes from another branch, then apply to my current branch.

And this usually happens, before branch merge.

$ git checkout $my_branch
$ git cherry-pick $git_revision

In below figure, we cherry-pick C D E from branch2 to branch1.


Read more: cherry-picking explained

Q: How to setup two remote repo urls for one local git repo?

Sometimes I need to push before I can test. So I may push quite often.

I don’t want to have too many tiny git pushes or too many push notifications for everyone.

So what I usually do? (Warning: this may not be a good practice for some projects, in terms of code security.)

Setup two remote repos for one local git repo. Keep pushing to one remote repo.

Once it’s fully tested, push to the official repo once.

$ git clone git@github.com:devops/denny-repo.git

$ git config remote.myremote.url git@bitbucket.org:devops/denny-repo.git
$ git config remote.origin.url git@github.com:devops/denny-repo.git

$ cat .git/config

$ git push origin master # origin points to github
$ git push bitbucket master # myremote points to bitbucket

Q: What is git stash?

Stash local changes without git commit or git push.

Switch to another branch. Then come back later.

# Shelve and restore incomplete changes

# Temporarily stores all modified tracked files
$ git stash

# Restores the most recently stashed files
$ git stash pop

# Lists all stashed changesets
$ git stash list

# Discards the most recently stashed changeset
$ git stash drop

Q: What is git rebase?

git-rebase: Reapply commits on top of another base tip

If you don’t care about your git commit history, you can skip “git rebase”.

But if you would prefer a clean, linear history free of unnecessary merge commits, you should reach for git rebase instead of git merge when integrating changes from another branch.

Read more: Merging vs Rebasing

Q: What is git squash?

You may have several local “git commit”.

Now run “git push”, it will generate several git commit history.

To consolidate them as one, we can use “git squash” technique.

# get local 3 latest commits
$ git rebase -i HEAD~3

# In editor, change "pick" to "squash". Keep the first one as "pick"

$ git rebase --continue
$ git push origin $branch_name

Read more: GIT: squash your latests commits into one

Q: What is git revert?

A fun metaphor is to think of Git as timeline management utility.
Commits are snapshots of a point in time or points of interest.
Additionally, multiple timelines can be managed through the use of branches.

When 'undoing' in Git, you are usually moving back in time,
or to another timeline where mistakes didn't happen.
  • [Undo local changes] When the changes haven’t been pushed yet

If I want to totally discard local changes, I will use git checkout.

When the repo is small, I might even run “git clone”, then git checkout.

$ git checkout $branch_name
  • [Undo public changes] When the changes have been pushed yet
$ git log -n 5
$ git revert $git_revision
$ git push origin $branch_name
  • How to undo a git pull?
CommandScopeCommon use cases
git resetCommit-levelthrow away uncommited changes
git resetFile-levelUnstage a file
git checkoutCommit-levelSwitch between branches or inspect old snapshots
git checkoutFile-levelDiscard changes in the working directory
git revertCommit-levelUndo commits in a public branch
git revertFile-level(N/A)

Read more: Undoing Commits & Changes

Q: What is git reflog?

Git maintains a list of checkpoints which can accessed using reflog.

You can use reflog to undo merges, recover lost commits or branches and a lot more.

Read more: git reflog

CheatSheet Of My Frequent Git Commands:

Show latest history with oneline for eachgit log -n 10 –oneline
Check git log by patternsgit log –grep=”$pattern”
Check git log by filesgit log – foo.py bar.py
Change the last commit messagegit commit –amend
Check git configuration for current repogit config –list
Delete local branchgit branch -D $branch
Delete remote branchgit push origin –delete $branch
Delete local taggit tag -d $tag
Delete remote taggit push –delete origin $tag

Posts: Tag #Git [display-posts tag=”git” posts_per_page=”20” orderby=”title”]


PRs Welcome

Blog URL: https://www.dennyzhang.com/git-concepts