As more and more network engineers shift towards the world of network automation, GIT is increasingly becoming a more and more important skill set on your resume. While is it not necessary to understand behind the scenes of git for a network engineer or the implementation details of the framework but it indeed is important to understand the concept behind GIT and how it fits in the world of networking. Let’s see some Git for Network Engineers.
Git is not Github or Gitlab. Git is a distributed version control system. It is software for tracking changes in any set of files, usually used for coordinating work among programmers who are collaborating on the same source code.
GitHub/GitLab on the other hand the web-based cloud offerings of GIT. These services use GIT in the background to offer a cloud / online repository to store your code securely so that it’s accessible to everyone no matter where they are and they can collectivity contribute on the same source code.
Fundaments of Git
- Git maintains three file systems also known as git structures also known as trees.
- Working directory / local workspace
- Store source code files, binaries, images, documentation, and whatever else you need.
- Staging area / index
- This is an intermediary storage area for items to be synchronized.
- Local repository
- store all committed items.
- Working directory / local workspace
- Git LifeCyle
- Untracked
- When you first create a file in a directory that Git is managing, it is given an untracked status. Git sees this file but does not perform any type of version control operations on it.Â
- Unmodified
- A tracked file in Git is included as part of the repository, and changes are watched. This status means Git is watching for any file changes that are made, but it doesnât see any yet because no modifications are done till now.
- Modified
- Whenever you add some code or make a change to the file, Git changes the status of the file to modified. Modified status is where Git sees that you are working on the file but you are not finished.
- Staged
- Once a changed file is added to the index(Staging Area, refer above), Git needs to be able to bundle up your changes and update the local repository. This process is called staging and is accomplished through git commit. At this point, your file status is moved back to the untracked status and start over a clean slate.
- Untracked
Git Workflow & basic commands for each stage
- git init
- Initialize a new local git repository. A new file created is by default untracked.
- git add
- Tell git to keep a track of this file and move it from untracked to tracked status.
- git status
- check the status of working directory from git’s perspective
- git commit
- After modifications have been done, move the file into staging area before it is finally merged into the local repository. This of this as candidate config before its merged to main router config.
- git remote add
- If you now intend to push the changes from local git repository on your machine to a remote git server like Github or Gitlab, you need to tell git the location of that repository. This command doesn’t actually push your code to remote repository, it only maps the location of the remote.
- git remote rm
- Remove the remote repository from your git project.
- git clone
- Clone an already created git repository. Basically a mirror image of remote git directory that can serve as your starting point. When you use the command git clone on a repository, Git automatically adds the remote repository connection information via the URL entered with the clone command
- git push
- In order for your code to be shared with the rest of your team or with the rest of the world, you have to tell Git to sync your local repository to the remote repository (on a shared server or service like GitHub)
- git pull
- syncs any changes that are on the remote repository and brings your local repository up to the same level as the remote one. Whenever you begin to work with Git, one of the first commands you want to issue is pull so you can get the latest code from the remote repository and work with the latest version of code from the master repository. git pull does two things: fetches the latest version of the remote master repository and merges it into the local repository.
- git fetch
- similar to git pull excep that it doesn’t merge anything to your local repository. You can still see what is being downloaded from the remote repository before you want to merge it to your local repository.
- git branch
- Branches or timelines are an important workflow in software development. Say you want to add a new feature to your software or want to fix a bug. You can create a branch in order to add a separate development workspace for your project and prevent changes from destabilizing the main project (the master branch in Git).
- git checkout
- While git branch creates a new branch but it doesn’t switch the head to the new branch. git checkout will move the head / pointer to the new branch.
- git merge
- The merge process in Git is used to handle the combining of multiple branches into one. If one network engineer added new ntp servers, they will most likely create a new branch, make changes to the existing code file. Similarly another network engineer will create another branch to probably submit a change request for updating the DNS servers. At the end of the day once all these changes are approved, they need to ported back to the main branch and merged into the main file so that they become persistent changes.
- git rm
- delete a git tracked file from repository
- git mv
- rename a git tracked file
- git diff
- This command highlights the differences between your working directory and the index (that is, what isnât yet staged).
There are tons of more git commands but these are the most basic and highly used command that should be enough to get you started with git
These screenshots below of all the commands discussed above will help in better understanding.
Understanding the merge process can be a little tricky and the snapshosts of merge in the abobve slideshow might not be clear enough. I will add a more detailed description on Understanding merge process in git.
Download and Basic Git Configuration
- Download and install GIT – Chose your OS and follow download/setup instructions
2. Configure Initial settings – Settings can be either GLOBAL ( for all your repositories ) or LOCAL which are per-repository or the current git directory you are in.
- Configure name and email. This is what Git will identify you as.
- Set default editor ( this is optional but i prefer using vscode ).
git config --global user.name "Gurpreet Kochar"
git config --global user.email "kochar.gurpreet@gmail.com"
git config --global core.editor "code --wait"
3. View your git config settings ( either of technique work just fine to view your settings )
git config --global -e
or
git config -l
or
git config --list
or
cat ~/.gitconfig
or
more ~/.gitconfig
4. Ready to start executing commands as discussed above.