Technical Blog Series: Part 1 - What is Git? - YuktaMedia

July 2nd, 2019

Technical Blog Series: Part 1 – What is Git?

Git is a version control system (VCS) for tracking changes in project directory and manage source code. It is free and open source. A complete long-term change history of every file. This means every change made by many individuals over the years. Git takes snapshots of a project, and stores those snapshots as unique versions. Git is designed for distributed development. If you’re involved with a project you can clone the project’s Git repository, and then work on it as if it was the only copy in existence. Every developer’s working copy of the code is also a repository that can contain the full history of all changes. 

If you want to install the basic Git tools, go through Git installation URL There are many interfaces available for Git like GitHub, GitLab, Savannah, BitBucket, and SourceForge etc.

Diagram: Basic-remote-workflow

 

To start with Git need to make project directory as Git repository usually we called it as ‘Local Repository’.

Initializing a new repository: git init

git init

To create a new repo, you’ll use the git init command. git init is a one-time command you use during the initial setup of a new repo. Executing this command will create a new .git sub-directory in your current working directory. This will also create a new master branch.

Add a remote repository. 

git remote add origin ssh://git@github.com/[username]/[repository-name].git

Git setup command:

After initializing Git in the directory you should link your username and email address to your Git profile. You can use the following command to do the same. 

git config –global user.name “FIRST_NAME LAST_NAME”

git config –global user.email “MY_NAME@example.com”

There are the level of configuration we can do, one is with –global option which basically works as the default configuration for all git repository setup on your system. Another one is with –local option which only applies to the repository where you are running this command. 

git config user.name “FIRST_NAME LAST_NAME”

git config user.email “MY_NAME@example.com”

Basic Git commands that usually needs to perform Git operations:

To Cloning an existing repository: git clone.

git clone /path/to/repository

It used to create a copy or clone of remote repositories. When executed, the latest version of the remote repo files on the master branch will be pulled down and added to a new folder.

Local changes commands:

  1. If files were modified then it will shows the list of files/directories which gets modified. Usually used to check the status of project files whether they modified or not.
git status
  1. Check the changes to tracked files. Used to check the difference between the last version of files and the current version of modified files. 
git diff
  1. Add current changes to the next commit. Used to add the newly added or modified files to current branch.
git add <file_path>
  1. To add all new and changed files to the staging area.
git add -A
  1. Commit staged local changes in tracked files. Used to commit the recently added files to the branch and save changes to project/repository. Options we can use with : ‘-m’ to specify commit message.
git commit
  1. To remove a file (or folder).
git rm -r [file-name.txt]
  1. To store our changes when they are not ready to be committed and we need to change to a different branch.
git stash
  1. Apply stash changes/files and drop on one command:
git stash pop
  1. If you added a file by mistake, you can unstage it (but keep local changes).
git reset HEAD path/to/file

Git Branching:

  1. To check current branch and to display the git branch names list available.(the asterisk denotes the current branch).
git branch
  1. To list all existing branches.(local and remote)
git branch -av
  1. To switch from one to another branch.
git checkout <branch name>
  1. To create and switch into new branch.
git checkout -b <new-branch name>
  1. To remove all the modified changes from the specified file.
git checkout <fileName/path>
  1. To clone a remote branch and switch to it.
git checkout -b [branch name] origin/[branch name]
  1. To delete the branch from local repository.
git branch -d <branch>
  1. To delete a branch on the remote.
git branch -dr <remote/branch>

 Or

git push origin –delete [branch name]

Git merging and rebase:

  1. To merge a branch into the active branch. It simply merges one branch into parent or master branch. When your new feature is ready, you might want to integrate it into another branch (e.g.: prod or testing branch). 

First, switch to the branch that is supposed to receive these changes. Then call “git merge” command with the name of the branch you want to integrate.

git merge <branch>
  1. To integrate changes from one branch into another. It is an alternative to the better known “merge” command.
git rebase <branch>
  1. Used to abort the rebasing operation.
git rebase –abort
  1. To Continues the rebasing process only when all the conflicts get resolved.
git rebase –continue

UPDATE & PUBLISH Commands:

  1. To pull the project/files/code from the remote location and to download and integrate remote changes. To integrate new changes from the remote repository. This will update your current HEAD branch with the new data from its counterpart branch on the remote. The changes will be directly merged into your local working directory.
  1. To publish the committed changes on remote location. Options we can use with : ‘-f’ to forcefully publish changes.
git push <remote> <branch>

UNDO git operations:

git reset changes, where your current branch is pointing. The difference between –hard and –soft is whether or not your index is also modified. So, if we’re on branch master with this series of commits:  – A – B – C (master)

HEAD points to C and the index matches C.

–SOFT:

When we run git reset –soft B, master (and thus HEAD) now points to B, but the index still has the changes from C; git status will show them as staged. So if we run git commit at this point, we’ll get a new commit with the same changes as C.

–hard:

It modifies your working directory. If we’re at C and run git reset –hard B, then the changes added in C, as well as any uncommitted changes you have, will be removed, and the files in your working copy will match commit B. Since you can permanently lose changes this way, you should always run git status before doing a hard reset to make sure your working directory is clean or that you’re okay with losing your uncommitted changes. 

  1. To Discard all local changes in your working directory. It removes all the files changes.
git reset –hard HEAD
  1. To Discard local changes in a specific file. Restores all the changes to file up to the last commit.
git checkout HEAD <file>
  1. Reset your HEAD pointer to a specific commit and discard all changes since then. Removes all the changes done after the commit ID specified in the command.
git reset –hard <commit>
  1. To remove the commit but keep the committed changes to the staged area on the local repository.
git reset <commit>

COMMIT HISTORY Commands:

  1. Show all commits, starting with latest commit information.
git log
  1. Show changes over time for a specific file.
git log -p <file>
  1. Who changed what and when in <file>.
git blame <file>
  1. Preview changes before merging.
git diff [source branch] [target branch]

Version Control Best Practices:

  1. Commit Related Changes: A commit should be a wrapper for related changes. For example, fixing two different bugs should produce two separate commits.
  2. Commit Often: It allows you to share your code more frequently with others. That way it’s easier for everyone to integrate changes regularly and avoid having merge conflicts.
  3. Test Before You Commit: Resist the temptation to commit something that you “think” is completed. Test it thoroughly to make sure it really is completed and has no side effects (as far as one can tell).
  4. Write Good Commit Messages: Begin your message with a short summary of your changes.
  5. Use Branches: Branches are the perfect tool to help you avoid mixing up different lines of development. You should use branches extensively in your development workflows: for new features, bug fixes, experiments, ideas…

References:

  1. https://www.atlassian.com/git/tutorials
  2. https://confluence.atlassian.com/bitbucketserver/basic-git-commands-776639767.html
  3. https://www.git-tower.com/blog/posts/git-cheat-sheet

If you have any queries, questions or suggestions, feel free to share on info@webtest.yuktamedia.com

Happy to help!

Leave a Reply

Your email address will not be published. Required fields are marked *

Aditya Bhelande

Aditya Bhelande

Aditya is the founder & managing partner at YuktaMedia. He has rich global experience in Digital Media having worked with large Publishers and Media Houses. That paired with his deep technical knowledge of enterprise systems has led him to create and lead YuktaOne Media ERP for Publishers.

person describing yuktaone's features

Discover how YuktaOne Media ERP addresses your Ad and Revenue Ops challenges

Schedule a free demo and learn from our Ad & Revenue operations experts.

TRY YUKTAONE FOR FREE

Trust their word for us.

YuktaMedia’s knowledgeable and experienced Ad Ops team has provided unparalleled support for Rotana sites by leveraging YuktaOne Media ERP for automated real-time reporting, revenue tracking, campaign pacing, performance and delivery.

Abir El Naboulsi

Sales Strategy Partner

“Working with the Yuktamedia team has given our company the extra support we needed to expand our business. The YuktaOne platform has reduced reporting hours significantly, allowing our internal team more time to focus on other initiatives. And our partners love having instant access to their data through a clean UI. We’ve worked with several outsourced teams over the years and Yuktamedia stands out because of their dedication to our success. It isn’t just about completing a task, Aditya’s team cares about producing quality results.”

Marisol Perez

Business Operations & Strategic
Partnerships | Digital Advertising

YuktaMedia has been an invaluable partner, helping us monitor our business KPIs with their platform. They build and maintain data connections quickly and they are very responsive to all our requests.

Jeffrey Wu

Senior Director, Global Operations

YuktaOne Media ERP has helped transform our client's business and has become an essential part of their ad and revenue operations. The Yukta team is knowledgeable, responsive and professional. We look to work with them whenever we can.

Stefanie Beach

Founder and CEO

Our beloved Clients

We're equiped to serve you here