git checkout commit_hash : just point the head to the commit
git branch -f branch_name hash : just take the branch to that commit
git checkout master and then git rebase bugFix : would set master to the line
of bugFix.
Git revert and reset:
On local work: git reset branch_name~n would remove n commits as if were not there
On remote: git revert HEAD on any branch would take that branch and make
removal of recent commit as new commit there.
Cherry picking:
git cherry-pick commit-1 commit-2 … : Means i would like to copy these
commits below current location HEAD.
Rebase interactive
rebase -i : choose options from vim or anywhere.
can do either of 3 things:
reorder
pick off : dropping the commit
squashing commits : merging into one
git rebase -i HEAD~4 : would let you rebase 4 commits\
Tags:
git tag tag_name commit_hash
git describe branch_name : it would describe in relation to tags: which is
easier to understand and go along.
New branch and checkout to before merge:
git checkout -b bugWork HEAD1^21: means first go to ancestor of
current_branch, choose the second parent to move up, then go back one commit
up and make it bugWork branch.
Git fetch:
git fetch performs two main steps, and two main steps only. It:
downloads the commits that the remote has but are missing from our local repository
updates where our remote branches point
Git fetch wont update master branch though.
So, it’s just downloading all remote work but not accepting it just with fetch.
Merging from remote after fetch:
using cherry-pick origin/master, rebase origin/master, merge origin/master
Git pull : exactly does this fetch and merge
Git pull –rebase
when pulling off from remote when you have been working on older version of remote.
Track origin/master with branch other than master:
git checkout -b dummy_branch origin/master; git pull : this will make
dummy_branch and track it to remote’s master branch.
Another way is to use: git branch -u origin/master dummy_branch
if currently we are in dummy_branch: git branch -u origin/master does the work
Tracking different remote branches:
git checkout commit_hash; git push origin master: this pushes the commit to
track origin/master.
Pushing one branch to another in remote:
git push origin source:destination : e.g. git push origin foo:master will set
the foo branch to track origin/master.
Pulling from one remote to another local:
git fetch remote_source:destination_local: eg. git fetch origin master^1:foo
will get ancestor of master to a new branch foo.
Source in push and fetch can be nothing:
Git abuses the parameter in two weird ways. These two abuses come
from the fact that you can technically specify “nothing” as a valid source for
both git push and git fetch.
Fetching nothing to local creates a new branch
Pushing nothing to remote removes the branch
Pull commands with source and destination:
they are just git fetch and merge in action.
Clearing branch history or certain commits
git checkout –orphan cleanup_branch_name
git add .
git commit -am “First commit”
git cherry-pick commit-name to commit-name : which commits you want to stay. If you want everything gone then no need to cherry pick
git branch -D master
git branch -m master : renames cleanup_branch_name to master.
git push origin master
if it doesn’t work
git push -f origin master
More Helpful Commands
Stash Untracked files too
git stash -u
Reflog
This shows all the reference records of everything that we have changed.
git reflog
git reflog show branch_name
The second command shows the reflog for a particular branch. Now to take the branch to a specific commit in the past, you can do:
git reset --hard commit_hash
Undoing
Including forgotten files
EGIN_SRC sh
add file_name
commit –amend –no-edit
ND_SRC
o this often. I forgot to add some static files or migrations in my commit. This will come handy.
Making a command alias for long command
EGIN_SRC sh
config –global alias.car ‘commit –amend –no-edit’
ND_SRC
s means it will make us not type the whole lengthy message to reuse the commit in making some amendments to it.
Reset the changes not committed
EGIN_SRC sh
checkout .
ND_SRC
cks us out to the most recent commit on that branch.
Remove a commit completely
EGIN_SRC sh
reset –soft HEAD^
ND_SRC
t reset means leave the files in the working directory and index untouched. ^ goes to parent commit of HEAD. This just takes us back to one commit before.
History
More informative log
git log --oneline --decorate --graph --all -30
The last one is the number of commits to show.
Find something on the log
git log --grep -E -i 'textpattern'
-E stands for extended regex and -i for case insensitive search.
Searching within commit code
git log -S "search_term"
Now you can find this search_term on the previously committed changed files as well.
Single file log
git log --oneline -- file_name
You get the log for specific file and see it’s history.
Blame
git blame file_name
See most recent commit that modified a given line, for every line in the file.
Show
git show commit_hash
By finding commit_hash from blame and log commands, we can use the commit_hash to compare to present changes.
Adding files with multiple changes
Often times you work on some file, you might not want to add all the changes at once. If you do git add file_name, it adds everything.
So to add changes iteratively:
git add --patch
Gives options to skip the hunch, split it, stage this, quit and so on.
Cherry Pick
Often times when you commit on the master branch than on your feature branch, we can cherry pick to copy commits across branches.
git diff origin/master..master
First we need to make sure the specified range is good to be copied.
Then we go to our branch using checkout -b branch_name and then use such command:
git cherry-pick origin/master..master
By doing this we cherry pick the needed commits to our branch.
Then go to master and remove the commits: