DevOps 6 min read

What is Git and how do you use it?

Written on 15 Jul 2021
Overview

Imagine you’re working on a coding project. You’re about to update the interface. At the same time as you’re working on your piece, someone is working on the login page, and someone else is working on the payment system. All three of you started from the same set of files. All three of you are submitting changes at the same time. What happens?

What is Git and how do you use it?
When it comes to anything but the most basic of coding projects, simply saving the files and copying them for backups is not adequate. This is where Git comes in.

Branches

Branches   
Developers can create a “branch”, an independent copy of the project to work on their changes. Once you’re ready, you can merge the branch back into another branch. It takes the parts you’ve changed and changes them in the master branch, meaning whatever has changed in the master branch since you made your branch will remain. If you experience problems after merging, that’s where the second most important part of git comes in-

Version control

Version control is more than just keeping backup copies of the code. It tracks what each developer changes when, and allows you to roll back individual changes without affecting other changes down the timeline. 

Distribution

In a way, Git allows you to make sure that you won’t likely lose everything if the central server goes down, as each developer working on the project likely has pulled a version of the entire project to their machine. We still recommend proper offsite backup, though.
Git allows many developers to work on a project simultaneously, and allows them to not bump into each other when working on different versions of the project. 

How to use Git?

You have two options- a command line, self hosted git (best for in-house projects) or using an online repository like GitHub or GitLab. The online options are great for public open source projects, especially if the contributors are a worldwide group of loosely connected people.
To use a local git, go to the official download page here
We’ll show you how to use it in Linux.
First, make a directory you want your project to reside in, then head inside it.  
mkdir test-project    
cd test-project
Now, create a repo or reinitialise an existing one
git init
Now, let’s create a text file to test with
git add testfile.txt
Note: If your project folder already has files, you can add them all with  git add. 
Make a commit
git commit -m "Initial" 
“Initial” is the commit message here, it can be anything.    
Add some context to the text file with your favorite editor. Now, let’s check it’s status. 
git status
You can see what files are modified with this command. It will show that you just modified our lonely little text file. Let’s add it to staging.
git add testfile.txt    
git commit -m "updating test file" 
You should be able to see this commit you just made by running
git log

Using Branches

We’ve been working in the master branch, which is the default behavior of git. Let’s add a new branch.
git branch testbranch
Now we need to switch to it, as we’re still pointing to the master. 
git checkout testbranch
Use your favorite editor and add more random text to demo.txt, and commit
git add demo.txt 
 
git commit -m "Testbranch update"
Now check the log for our new branch. 
git log
Since we’re ahead of the main branch, let’s merge it. First, point back at the master branch.
git checkout master
Now merge testbranch with master.
git merge testbranch
Finally, check the log again and you should see your successful merge. 
While this project of course had no conflicts with the merge, you’ll run into merge conflicts all the time when working on a real project.

Solving Merge Conflicts

Resolving conflicts is really different for each case, and you’ll get the hang of it the more you use it. Don’t be afraid to ask your colleagues to help you, it’s better than causing problems for everyone else by doing it wrong. 
Merge conflicts happen when you attempt a merge and Git is unable to resolve the differences in code between multiple commits automatically. Git can only automatically merge code if the changes are on different lines of code.

Things that can cause merge conflicts 

-Pending changes to the branch being merged to
-Conflict between the branch being merged and the branch being merged to. Git will resolve what it can, but there will be things that need to be resolved manually. 
For basic conflicts, you can make changes to conflicted files, then use the ‘git add’ command to stage the new merged code, create a new commit with ‘git commit’. Git will create a new merge commit. 

Commands to know when merging

git log –merge:  This will create a list of commits that are causing conflicts
git diff: Display the differences between the state of a repository or individual files 
git checkout: Undo the changes made to a file, navigate amongst all of the branches
git reset –mixed: Undo the changes to the working directory and the staging area 
git merge –abort: Abort the merge process, revert to the state before merging 
git reset: Point your local repository, staging area, and working directory back to a previous commit
git pull –rebase: Apply your local changes on top of the remote changes
git rebase –continue: Resume a rebase after resolving the conflict manually and updating the index with the desired resolution
git rebase –skip: Skip adding a conflicting commit
git mergetool: A tool that opens your chosen editor to show conflicts and resolve them by hand

To Git or to GitHub…

We hope this has been a helpful intro to one of the most important tools for any programming project. If all of this seems overwhelming to do locally in command line, you might want to consider using an online repo like GitHub. Read more about the advantages and disadvantages of using GitHub.
software developer jobs
If you’re an aspiring developer and you’re looking for a new career, we’re hiring! We have software developer jobs available in Fort Wayne, Kolkata, and Wellington.

Share this article

966 reads
Contents

Similar Reads