Git Commands
Branches
Show branch details
$git branch -v -a
$git branch -a
$git branch -r
$git remote show origin
List revision
$git rev-list -n 2 HEAD -- <file_path>
Retrieve new branch
$git fetch origin
$git fetch origin --prune
Create Branch
$git checkout -b test origin/test
Set upstream
$git branch --set-upstream-to=origin/<BRANCH> <BRANCH>
Push to Origin
$git push -u origin <BRANCH>
Merge
$git merge --no-ff <BRANCH>
Reset
$git fetch origin
$git reset --hard origin/master
Delete
delete local branch:
$git branch -d testing
$git branch -D testing (force)
delete remote:
$git push origin :<BRANCH> or :refs/heads/<BRANCH>
Other
Undelete file
if file was removed without commit.
$git reset <file>
else
$git checkout <file> ??
Get revision
$git checkout <revision>^ -- <file_path>
Get commit hash
long
git rev-parse --verify HEAD
short
git rev-parse --short HEAD
Rebase (update to one commit).
$git update-ref -m "reset: Reset <branch> to <new commit>" refs/heads/<branch> <commit>
Merge/cherry pick
$git cherry-pick b50788b
(note: don't forget to do a pull locally from the origin branch)
Misc
Create Branch and push to Remote
$git checkout -b test origin/test
$git push -u origin <BRANCH>
Set upstream
$git branch --set-upstream-to=origin/<BRANCH> <BRANCH>
Change remote
git remote rm origin
$ git remote add origin https://github.com/user/repo.git
# Set a new remote
$ git remote -v
# Verify new remote
> origin https://github.com/user/repo.git (fetch)
> origin https://github.com/user/repo.git (push)
Commits
-m
Sets the commit's message. Make sure to provide a concise description that helps your teammates (and yourself) understand what happened.
-a
Includes all currently changed files in this commit. Keep in mind, however, that untracked (new) files are not included.
--amend
Rewrites the very last commit with any currently staged changes and/or a new commit message. Git will rewrite the last commit and effectively replace it with the amended one. Note that such a rewriting of commits should only be performed on commits that have not been pushed to a remote repository, yet.