Practical Git

Git Version Control System

Using Git for your code, scripts and configuration storage are clearly best practice this days. This is how you store your code and all your configurations.

Version control is one of a set of capabilities that drive higher software delivery, service delivery and organizational performance.

Version control systems like Git provides a simple way to organize files and coordinate their creation, access, and deletion across teams and organizations.

Improved software delivery

In order to improve software delivery, teams need to use version control for scripting, source code, tests, infrastructure and application configuration information, and the many libraries and packages they depend upon.

Research shows that comprehensive use of version control, among other capabilities, predicts continuous delivery. In particular, version control helps you meet these critical requirements:

Reproducibility

Teams must be able to provision any environment in a fully automated fashion, and know that any new environment reproduced from the same configuration is identical.

Traceability

Teams should be able to pick any environment and determine quickly and precisely the versions of every dependency used to create that environment. They should also be able to compare two versions of an environment and see what has changed between them.

It is worth noting that both Microsoft and Google recommends Git technology. With the acquisition of GitHub in 2018, Microsoft has clearly signaled that it agrees with the rest of the industry that Git is the version control system for the modern era.

There are many exelent tutorials and guides out there how to use basic Git. I will really encurrage you to be an advanced Git user. This is not a tutorial but more a list to remember all the different Git commands I use everyday.

Git Inspiration and Guidences

Git Commands That I Use Almost Daily

Using Branches in Git

Command Description
git fetch --all Fetch all branches from all remotes
git branch -a To see all the branches
git remote prune origin Remove tracking branches no longer on remote
git branch -vv Delete branches marked with gone

New branch on Remote - Import folder

The first commit made on this new branch will have no parents and it will be the root of a new history totally disconnected (orphaned) from all the other branches and commits.

1git init
2git checkout --orphan vmtest
3git remote add origin git@github.com:astbss/Security.git
4git push -u origin vmtest
1git add .
2git commit -am "First Commit"

New branch without history

The first commit made on this new branch will have no parents and it will be the root of a new history totally disconnected (orphaned) from all the other branches and commits.

1git checkout --orphan <new_branch>
2git rm -rf .
3rm -rf *

You might want to do this to publish an open source branch of a project whose current tree is "clean", but whose full history contains proprietary or otherwise encumbered bits of code.

If you want to start a disconnected history that records a set of paths that is totally different from the one of <start_point>, then you should clear the index and the working tree right after creating the orphan branch by running this from the top level of the working tree.

1git rm -rf .

Git create a branch from another branch

1git clone git@github.com:astbss/Security.git
2git clone -b branchname git@github.com:astbss/teambbb.git arerssfeed
3git checkout -b arerssfeed
4git push --set-upstream origin arerssfeed

Git replace a branch with another branch

1git fetch --all
2git checkout areh2 #  You will replace this branch
3git reset --hard data # This is the branch you replace it with
4git push --force

Create Branch from commit id

You can checkout the commit ID and create a branch off of that commit point

1git checkout -b data-are3 4a3aca892f36104bd97036046c998f615b9c0140
2git checkout -b branch_name <commit id>
3git push --set-upstream origin data-are3

Remove tracking branches no longer on remote

Command Description
git remote prune origin Prunes tracking branches not on the remote.
git branch --merged Lists branches that have been merged into the current branch.

Git Automatic Merge using cli

Step 1: From your project repository, bring in the changes and test.

1git fetch origin
2git checkout -b dashboard origin/dashboard
3git merge are2

Step 2: Merge the changes and update on GitHub.

1git checkout are2
2git merge --no-ff dashboard
3git push origin are2

Partial Merge one file

1git checkout -p  superbranch cosmos_utils.py

Merge development branch with master or another branch

 1 cd teamsop
 2 # do git pull on master and aresbox and make it up-to-date
 3 git checkout aresbox
 4 # Merge master into the development first so that if there are any conflicts,
 5 # I can resolve in the development branch itself and  my master remains clean.
 6 git merge master
 7 # (resolve any merge conflicts if there are any)
 8 git push
 9 git checkout master
10 # If you want to keep track of who did the merge and when, you can use --no-ff flag while merging to do so.
11 # This is generally  useful only when merging development into the master (last step),
12 git merge --no-ff aresbox # (there won't be any conflicts now)
13 git push # publish your local commits back to origin/master

How to "merge" specific files from another branch

We can simply give git checkout the name of the feature branch1 and the paths to the specific files that we want to add to our branch.

1git checkout my-backend mypython/process_python_rest.py
2git checkout my-backend .gitignore

Merge --no-commit and --abort

arebranch have preference and we can see what the merge have added to evgaaai

1git merge --no-commit --no-ff evgaaai  # Use This
2git diff evgaaai arebranch -- getxxxdata.py
1git merge --no-commit --no-ff -s ours evgaaai
2git merge -X theirs arebranch
3git merge --abort  # Just in case you want to abort. If not just commit
4git reset --hard origin/arebranch  # Alternative reset

merge --squash

This will take all the commits from the bugfix branch, squash them into 1 commit, and merge it with your master branch.

1git checkout master
2git merge --squash bugfix
3git commit

Merge To checkout your own version

1git checkout HEAD -- <filename>

List all modified files in git merge commit

1git log -m --name-only
2git log -m --name-status
3git diff --name-status <SHA>^1 <SHA>
4git diff --name-only <SHA>^1 <SHA>