0% found this document useful (0 votes)
170 views

8 Useful But Not Well-Known Git Concepts: Checkout Cherry-Pick

Git provides several useful but lesser known concepts for advanced usage, including .gitkeep files to track empty directories, cherry-picking changes between branches, setting up multiple remote repositories, stashing incomplete changes, rebasing and squashing commits, reverting commits, and using reflog to undo actions.

Uploaded by

cap.rohit550
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
170 views

8 Useful But Not Well-Known Git Concepts: Checkout Cherry-Pick

Git provides several useful but lesser known concepts for advanced usage, including .gitkeep files to track empty directories, cherry-picking changes between branches, setting up multiple remote repositories, stashing incomplete changes, rebasing and squashing commits, reverting commits, and using reflog to undo actions.

Uploaded by

cap.rohit550
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

8 Useful but Not Well-Known Git Concepts

For advanced Git usage, I usually leverage the GUI of GitHub or


BitBucket. But the 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 the .gitkeep file?


You may know.gitignore very well. But what is .gitkeep?
By default, Git doesn’t track empty directories. A file must exist within it.
We can use the .gitkeep file to add empty directories into a git repo.

Q: What’s git cherry-pick?


Pick changes from another branch, then apply them to the current
branch.
And this usually happens, before branch merge.

$ git checkout $my_branch


$ git cherry-pick $git_revision

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


Q: How to set up 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.)
Set up 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.

Q: What is git squash?


You may have several local git commits. Now run “git push,” and it will
generate several git commit histories. To consolidate them as one, we can
use the “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

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?

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.

Cheat sheet of my frequent Git commands:

You might also like