14. Git Basic Commands

Git is a SCM (Source Code Management) Software.

14.1 Branch

A branch is simply a pointer (=alias/alternative_name/shorthand_name/) to a commit. Hence, branch just adds a extra name to existing commit-hash. So, every branch has a base (parent) branch. (but when main was created first time, it had no parent.) if we create a branch, a reference file is created here: ./git/refs/heads

HEAD is name given to present branch (if no branch, then commit hash). If present commit has a branch name associated with it, then HEAD points to the branch name. (branch name itself is alias to a commit(hash), so all is same) But if the present commit object does not have a branch, it will point to the hash. In other words, a branch(name) is simply a human readable alias given to a commit-object. we can do

cat .git/HEAD

head (lower case) is another term used in git. It simply means top of a branch. So, we may have many branches, so many heads!

14.1.1 branch command

git branch [-a] [-r] [-v] [-vv]
-a : list both local branches and remote tracking branches
-r : list only remote branches cached locally
-v : list verbose branch report
-vv: list local branch and along with each branches remote tracking relation.
git branch [-u] <local_branch_name>

Create a new local branch from current commit/branch.
-u : remote tracking branch; Means, when we push/fetch, we tell, use same branch name for local and remote.
When push/fetch/pull don't need remote branch name. We can use different branch name for remote, but that is not a good idea. So use same branch name at local and remote. Any way, there is PR to merge

git branch --delete [--force] <branch_name>

--delete: Delete a local branch if and only if it is fully merged in the remote.
--force: delete it even if is not merged in remote.

14.1.2 rename

git branch -m rename local old branch to new branch

14.1.3 Create

To create a new branch (Always, with current branch as the base) and switch to new branch.

git switch -c <branch_name>
OR 
git checkout -b <branch_name>
OR 
To just to create but not to switch
git branch <branch_name>

14.1.4 Delete

git branch -D

14.2 push

14.2.1 form - 1

A simplified push command looks like below.
push the ref_spec to remote

git push [options] [[remote]  [ref_spec]]

Where

options  
--delete : delete the ref_spec on remote. This is same as not mentioning the src in ref_spec. (see below)
--tags   : push all tags in (local) refs/tags along with whatever mentioned in the ref_spec.  
           so, we need to be sure, what tags we created locally. If we do not want all tags to go in   
           one shot, we can simply mention a known tag ref_spec

ref_spec: [  [+][<src>][:<dst>]  ]

The whole ref_spec is optional for many when options like -u, --tags are used. But it is 
essentially src and dst are regular expressions.  

They can represent branch names, tags, commit-hash.
Note: we tend to think, it represents only branch name.  It is not true.

src will be searched in local repo refs/heads or refs/tags  
dst will be searched in remote repo ref/heads or refs/tags  

if dst does not start with refs/heads or refs/tags, and if src if found in the local refs/heads or 
ref/tags, prepend that path to dst so that, it can be properly searched at remote.

<src> points to local ref
<dst> points to remote ref
so, push src to dst.
if :dst is omitted, it is assumed same as src
if src is omitted, it means, push the 'null' src to dst. this implies,  
dst points to null, hence, dst gets deleted.

So, effectivly, dst will be deleted.  
This is as using --delete option

For +, read below

TODO: + or --force

14.3 Delete branch

14.3.1 Delete local branch

14.3.2 Delete remote branch

#git push remote_repo --delete branch
git push origin --delete mybranch

14.4 References