Advanced Git Functions¶
aliases¶
- Show all commits since last release
changelog = "!f() { r=${1:-`git describe --tags --abbrev=0`..HEAD}; echo Changelog for $r; git log --reverse --no-merges --format='* %s' $r; }; f"
I made a mistake!¶
I want to Undo¶
- If the commits are still on the local repo and has not been pushed to the remote repo
git reset: resets the head of the branch to a previous commit
- If the commits have been pushed to the remote repo, then using
git revertis advisable as it leaves a record for other users in case they have already cloned your commits
git revert: goes back to previous commit, but does so by creating a new commit on the tree (Read more about pitfalls on Git-SCM)
I have a detached git HEAD!¶
Tip: Git concepts of HEAD, master, origin
HEAD: the current commit your repo is on. Most of the time HEAD points to the latest commit in your current branch, but that doesn't have to be the case. HEAD really just means "what is my repo currently pointing at".
In the event that the commit HEAD refers to is not the tip of any branch, this is called a "detached head".
master: the name of the default branch that git creates for you when first creating a repo. In most cases, "master" means "the main branch". Most shops have everyone pushing to master, and master is considered the definitive view of the repo. But it's also common for release branches to be made off of master for releasing. Your local repo has its own master branch, that almost always follows the master of a remote repo.
origin: the default name that git gives to your main remote repo. Your box has its own repo, and you most likely push out to some remote repo that you and all your coworkers push to. That remote repo is almost always called origin, but it doesn't have to be.
HEAD is an official notion in git. HEAD always has a well-defined meaning. master and origin are common names usually used in git, but they don't have to be.
Source : stackoverflow
If you have a commit that is not on any branch, it means that you have a detached git HEAD
Steps to merge a detached commit:
- Identify the commit hash of the recent commit:
git log -n 1 - Move to the master branch:
git checkout master - Save the changes on the detached
HEADto a temporary branchgit branch temp <commit-hash> - When you are ready to merge temp branch to the master:
git merge tempwhen you are on mastergit checkout master - If you would like to delete the temperorary branch:
git branch -d temp
Miscellaneous¶
-
Selectively merge changes from other branches: git cherry-pick StackOverflow,
-
Copy files from another branch/commit:
git checkout --patch B f(B: branch, f: filename)
-p or -patch: Interactively select hunks in the difference between the
Retrieve single file from old commit
git checkout [revision_hash] [file_name]
- Undoing things (Git book)