if you want to create a new branch AND check it out at the same time, you can simply type
git checkout -b [yourbranchname]
Make branch and checkout
git branch [yourbranchname] git checkout [yourbranchname]
Merge main to bugfix
git checkout bugfix git merge main
Rebasing essentially takes a set of commits, “copies” them, and plops them down somewhere else.
HEAD is the symbolic name for the currently checked out commit
Detaching HEAD just means attaching it to a commit instead of a branch.
Detach HEAD by checkout the associated hash value
git checkout [hash]
To see the hash:
git log
Go up the commit tree one at a time, use ^
git checkout HEAD^ //case sensitive
Go up a number at a time, use ~<num>
git checkout HEAD~4
You can directly reassign a branch to a commit with the -f option. -f is the force option. The following
moves (by force) the main branch to three parents behind HEAD.
git branch -f main HEAD~3
There are two primary ways to undo changes in Git — one is using git reset and the other is using git revert.
git reset reverses changes by moving a branch reference backwards in time to an older commit. The following reset/revert one commit
git reset HEAD~1 git revert HEAD
A very straightforward way of copy a series of commits below the current location (HEAD) in a branch. Note: None of the commits appended can be an ancestor of the HEAD.
git cherry-pick Commit1 Commit2 ...
All interactive rebase means is using the rebase command with the -i option.
If you include this option, git will open up a UI to show you which commits are about to be copied below the target of the rebase. It also shows their commit hashes and messages, which is great for getting a bearing on what’s what.
Example bring previous 4 commits into an UI for manipulation.
git rebase -i HEAD~4
Make slight modification to commit
git commit --amend
the command you’ll use to create local copies of remote repositories
git clone
This brings our local representation of the remote repository into synchronization with what the actual remote repository looks like (right now). It does not change anything about your local state. It will not update your main branch or change anything about how your file system looks right now.
git fetch
git pull
is essentially shorthand for a git fetch
followed by a merge of whatever branch was just fetched.
The behavior of git push
with no arguments varies depending on one of git’s settings called push.default
To make sure history sync with remote before pushing:
git fetch; git rebase origin/branch; git push git fetch; git merge origin/branch; git push
git pull --rebase
is shorthand for a fetch and a rebase
git checkout -b totallyNotMaster origin/master
set totallyNotMaster branch’s default remote track to master
Another way to set remote tracking on a branch is to use the git branch -u option. Running
git branch -u origin/master foo
Rename both local and remote branch:
git checkout old_name git branch -m new_name git push origin -u new_name git push origin --delete old_name
Reset to hash commit point for local and remote
git switch branch_name git pull git reset --hard 191f043 git push -f origin branch_name
Delete branch locally
git branch -d localBranchName
Delete branch remotely
git push origin --delete remoteBranchName
Branch off anther branch like dev
git switch dev git pull git git checkout -b myFeature dev
Syn remote version
git remote -v //this gives the remote source git remote set-url origin (https url of clone)
Sync dev to master
//make backup dev = devbackup-date git switch dev git pull origin dev git checkout -b devbackup-date //remove branch restriction from bitbucket //delete local dev branch git branch -d dev git push origin --delete dev //rebranch dev git switch master git pull origin git checkout -b dev //reinstate bitbucket restriction
Reset to an older commit. This will clear all commits before hash off the tree.
git checkout branch git reset hash -hard git push -f