What is Git?
Version control, also known as source control, is the practice of tracking and managing changes to software code. Version control software keeps track of every modification to the code in a special kind of database. If a mistake is made, developers can turn back the clock and compare earlier versions of the code to help fix the mistake.
Git is a modern version control system. Git is the de facto standard. It is the most broadly adopted tool of its kind.
Git includes the use of local repositories on developer systems and a remote repository like GitHub or Bitbucket for collaboration with other developers.
Git Tutorial
You can checkout the quick git tutorial video given below or read more in depth here.
Installing Git
Git is installed differently for Mac and Windows:
Mac
- Install homebrew. Use commandline installation: https://brew.sh/
- Install zsh: Use commandline installation https://ohmyz.sh/
- Verify the installation was successful by typing in terminal:
git --version - Configure your Git username and email using the following 2 commands, replacing Emma’s name with your own. These details will be associated with any commits that you create:
git config --global user.name "Emma Paris" git config --global user.email "eparis@optiphoenix.com"
Windows
- Download the latest Git for Windows installer.
- When you’ve successfully started the installer, you should see the Git Setup wizard screen. Follow the Next and Finish prompts to complete the installation. The default options are pretty sensible for most users.
- Open a Command Prompt (or Git Bash if during installation you elected not to use Git from the Windows Command Prompt).
- Configure your Git username and email using the following 2 commands, replacing Emma’s name with your own. These details will be associated with any commits that you create:
git config --global user.name "Emma Paris" git config --global user.email "eparis@optiphoenix.com"
Set up OptiPhoenix Repositories
Now that git is installed, let’s setup the repo in our system.
- Create a folder named something like optiphoenix where all the code and other repositories will go.
- Generate a SSH key.
- Add your SSH to Github or Bitbucket account that has access to the remote repos.
- Create a folder for the repo inside the optiphoenix folder.
- Open this new folder in the terminal.
- Get the remote repo URL.
- Clone the repo inside the folder using
git clone <repo_URL>
Our Git flow
Different repositories may have a different work flow. Check with your PM or mentor which to follow. Here’s one of the ways we use Git for our campaigns at OptiPhoenix to give you an idea:
- In the master or main branch, pull the latest changes using
git pull. - Checkout to the campaign branch.
- Pull changes from remote campaign branch if any using
git pull origin <branch_name>so it is in sync. - Make your changes in the code.
- Add all your changes using
git add .or specific changes usinggit add <folder_or_file_name>. - Commit all your changes.
- Push your changes to remote branch using
git push origin <branch_name>.
Branches
Different campaigns are created in different branches in the git repository.
- Once you’re assigned a campaign, get the campaign ID like SUBT11, DOM-6.4, PD_RT4U_14, etc.
- Create a new branch by the name of this campaign ID using
git checkout -b "branchName". - Make sure to add this branch name in the Trello card description.
You need to follow the naming convention of the campaign IDs, but what if you named it incorrectly? Read this guide to learn how to rename branches.
Commit Messages
When we commit our changes, we add a message along with it which tells us about the changes in that particular commit. We have created a standard that you need to follow for your commit messages here at OptiPhoenix. Make sure you read about this standard here.
Undo changes
A fun metaphor is to think of Git as a timeline management utility. Commits are snapshots of a point in time along the timeline of a project’s history. When undoing in Git, you are usually moving back in time.
Find what to undo
Once you’ve built up a project history of commits, you can review and revisit any commit in the history using the git log command. You can either find what commit to undo by the commit message or by loading the snapshot commit. Each commit has a unique SHA-1 identifying hash. These IDs are used to travel through the committed timeline and revisit commits.
When you have found a commit reference to the point in history you want to visit, you can utilize the git checkout <SHA-1 hash> command to visit that commit. After loading the commit, you can open and check the files in that branch how they looked after that commit.
To go back to the current state, checkout to the current branch: git checkout <branch name>.
Revert changes
When we use command like git revert HEAD, it creates a new commit which is the inverse of the last commit. Instead of deleting the last commit, it keeps the last commit and adds a new commit which is the inverse of the last commit changes. This is the ideal undo method.
Reset changes
When we use command like git reset --hard <SHA-1 hash>, the commit with that specific hash is deleted completely. These commits no longer exist in the commit history. At this point, we can continue working and creating new commits as if the ‘crazy’ commits never happened. This method of undoing changes has the cleanest effect on history. Doing a reset is great for local changes however it adds complications when working with a shared remote repository. For example, if we have a shared remote repository that has a 872fa7e commit pushed to it, and we try to git push a branch where we have reset the history, Git will catch this and throw an error. Git will assume that the branch being pushed is not up to date because of it’s missing commits. In these scenarios, git revert should be the preferred undo method.
Unstage changes
Now suppose you just made some error in your code or placed an unwanted file inside the repository, you can unstage the files you just added using: git reset HEAD~1
Cheat sheet
Use this handy Git cheat sheet guide to enhance your workflow. This Git cheat sheet saves you time when you just can’t remember what a command is or don’t want to use git help in the command line. It is hard to memorize all the important Git commands by heart, so print this out or save it to your desktop to resort to when you get stuck. This includes the basic Git commands to help you learn Git, and more advanced concepts around Git branches, remote repositories, undoing changes, and more.