avatarCassius

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

6421

Abstract

d="1ce5">Say we want to add <code>main.py</code> we can run</p><div id="0dd4"><pre>git <span class="hljs-built_in">add</span> main.py</pre></div><p id="4e0e">Then if we run <code>git status</code> again:</p><figure id="a9d8"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*jQo-KsM2hD4PKP6eSqQYxg.png"><figcaption></figcaption></figure><p id="ec5c">You can now see that <code>main.py</code> is now pushed into the staging area ready to be committed. Furthermore, you can see that <code>.gitignore</code> is still currently untracked.</p><p id="f240">Add all files to the staging area:</p><div id="31af"><pre>git <span class="hljs-built_in">add</span> -A</pre></div><figure id="a604"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*ENO8C9tD4qZYdSfB_9HgTg.png"><figcaption></figcaption></figure><p id="c8fe">Perfect! Now both files have been added to the staging area.</p><h2 id="af40">Removing files from the Staging Area</h2><p id="cd45">If you want to remove files from the staging area, you can use the <code>git reset</code> command. For instance, if you want to remove <code>main.py</code> from the staging area:</p><div id="1cb7"><pre>git reset <span class="hljs-selector-tag">main</span><span class="hljs-selector-class">.py</span></pre></div><p id="2c77">If you want to remove all files from the staging area, you can simply type:</p><div id="04d3"><pre><span class="hljs-attribute">git reset</span></pre></div><h2 id="2db9">Commit from Staging Area to the Repository</h2><p id="b205">Now that we have the files we need in the staging area, we are going to commit them to the repository.</p><p id="975b">We commit by typing:</p><div id="2965"><pre><span class="hljs-attribute">git</span> commit -m <span class="hljs-string">"type a message here"</span></pre></div><p id="37b6">Make sure your message makes sense for future reference. It is good to be precise on exactly what changes you have made.</p><p id="88a5">This is our first commit.</p><figure id="ebe1"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*w0A0FScE208RdnvnxdSCLw.png"><figcaption></figcaption></figure><p id="8d78">Now when we run <code>git status</code> we find there is now nothing in the staging area.</p><figure id="8b6b"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*MPjT9Bz93lmeRx_pDlhhyQ.png"><figcaption></figcaption></figure><p id="5eb5">We can also check the commits we have made by looking at the log.</p><div id="69be"><pre>git <span class="hljs-built_in">log</span></pre></div><p id="bd67">Sometimes Windows Explorer doesn’t show the <code>.git</code> folder. If that is the case for you, you need to set it so that you are able to see hidden files. You can do this by following the instructions in the link below.</p><div id="d5b6" class="link-block"> <a href="https://support.microsoft.com/en-us/windows/view-hidden-files-and-folders-in-windows-10-97fbc472-c603-9d90-91d0-1166d1d9f4b5"> <div> <div> <h2>View hidden files and folders in Windows 10</h2> <div><h3>View hidden files and folders in Windows 10</h3></div> <div><p> View hidden files and folders in Windows 10support.microsoft.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/)"></div> </div> </div> </a> </div><p id="33a4">So far, we have explored all three Git states on our local repository. Often we work in teams of more than one. Next we need to know how to copy a remote repository. This is what the next section is all about.</p><h1 id="2718">Cloning a Remote Repository</h1><p id="5faa">In real life, when you’re working in a team, you will need to clone an remote repository onto your local machine. You can do this with the following command:</p><div id="36d9"><pre>git <span class="hljs-keyword">clone</span> <span class="hljs-title"><url</span> <span class="hljs-keyword">or</span> directory></pre></div><p id="ceaf">If you’re simply starting out, you might not have a remote repository to clone from. So this is what we’re going to do instead.</p><p id="cf31">You’re going to create another folder in your drive called <code>clone/</code></p><p id="54ba">And you’re going to clone from one directory to another. In my case:</p><figure id="c61d"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*__829VDGvQ9zetSBClT2YQ.png"><figcaption></figcaption></figure><p id="1f55">This is because my original repo was in a folder called ‘sample’. Yours would be different of course.</p><p id="4403">Now you should be able to see a clone of your ‘not so remote’ repository.</p><p id="1000">If you type in the following command:</p><div id="5003"><pre><span class="hljs-attribute">git remote -v</span></pre></div><p id="f600">This will give you information on your local repository. Furthermore,</p><div id="6308"><pre>git branch -<span class="hljs-selector-tag">a</span></pre></div><p id="6609">This will list all the branches, locally and remotely for this project.</p><h2 id="ec36">Making a Change</h2><p id="42b8">Let us now make a change to a file in the clone repository. After making a change to a file, you can run:</p><div id="8355"><pre><span class="hljs-attribute">git diff</span></pre></div><p id="2279">This is list all the changes to the existing files you have made.</p><p id="5264">If you add a new file to the cloned repo, you can run the below to see new files and modified files.</p><div id="f251"><pre><span class="hljs-attribute">git status</span></pre></div><figure id="07ab"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*1CQI1pXPDKy1u0H-z5zzRQ.png"><figcaption></figcaption></figure><p id="34e7">Now we need to add them to the staging area by running:</p><div id="7117"><pre>git <span class="hljs-built_in">add</span> -A</pre></div><p id="3e34">Then we can run something like this to commit from the staging area:</p><div id="5cbb"><pre><span class="hljs-attribute">git</span> commit -m <span class="hljs-string">"newChange file added"</span></pre></div><figure id="6b28"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*6d0kJM1nIYh51GmZyQ-x9A.png"><figcaption></figcaption></figure><p id="2f98">Ok, so now that we have made the changes locally, we’re going to push these changes up to

Options

the remote repository so others can see.</p><h2 id="f6bb">Pushing it Back up to the Remote Repository</h2><p id="56d1">Before we push things up to the remote repository, we need to pull from it to make sure that during the time we’ve been working on our own changes, there hasn’t been any changes in the remote repository that we cloned from.</p><div id="1256"><pre>git pull origin <span class="hljs-literal">master</span></pre></div><figure id="fc38"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*bIMCM8YZzQTMwGZUxTa82A.png"><figcaption></figcaption></figure><p id="8fc5">If things are good, we will get a reply that everything is already up to date. Now we can push.</p><div id="2cc6"><pre>git <span class="hljs-built_in">push</span> <span class="hljs-built_in">origin</span> master</pre></div><p id="0c5b">Now you have pushed your changes back up to the remote repository!</p><h1 id="c921">Branching</h1><p id="c588">Often when you’re working on a large project, different aspects of the project can be worked on it different branches. Different users can also work on different branches. Branches can later be merged.</p><p id="a7a3">To check the branches you currently have:</p><div id="fd4b"><pre><span class="hljs-attribute">git branch</span></pre></div><p id="35e6">Most likely, you will only have the master branch.</p><p id="508c">To add a new branch you can simply type:</p><div id="72c1"><pre><span class="hljs-attribute">git branch newBranch</span></pre></div><figure id="9ff1"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*6MfxtflmbGDtpNdyDRUswQ.png"><figcaption></figcaption></figure><p id="4e97">The one with the asterisk on is the one that you’re currently working in.</p><p id="66a1">If you want to switch branches you can see the <code>checkout</code> verb.</p><div id="8049"><pre><span class="hljs-attribute">git checkout newBranch</span></pre></div><figure id="596d"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*PT5ag8a1XAPgiPsh8udkcg.png"><figcaption></figcaption></figure><p id="b34c">This will switch you to the new branch!</p><p id="a138">Now you can go ahead and make some changes to your files. You can see the changes you made by <code>git status</code> .</p><p id="abf3">After making some changes, you can commit to your new branch</p><div id="a197"><pre>git <span class="hljs-keyword">add</span> -A git <span class="hljs-keyword">commit</span> -m "changes to new branch ... blah"</pre></div><p id="13d5">After we have done that, we can push our new branch into our remote repository.</p><div id="0875"><pre>git <span class="hljs-built_in">push</span> -u <span class="hljs-built_in">origin</span> newBranch</pre></div><p id="6ffb">Now if we run <code>git branch -a</code> , we can see all the branches (local and remote):</p><figure id="c385"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*cfTfTcWDXRjapUwuRZBRzg.png"><figcaption></figcaption></figure><p id="2f66">You can see that our local newBranch has been pushed up to the remote repository.</p><p id="abd2">The idea of this is that others can review the changes you have made in the branch, run unit testing etc. Once everything is OK, you can merge the newBranch to the master. In the next section, we will be talking about merging branches.</p><h1 id="ab7e">Merge Branches</h1><p id="f248">To merge branches, you first have to merge your branch locally with your local master. Then you can push this up to the remote repository.</p><p id="e3ee">So first, you need to be in your local master:</p><div id="ca33"><pre>git checkout <span class="hljs-keyword">master</span> <span class="hljs-title">git</span> pull origin <span class="hljs-literal">master</span></pre></div><p id="c46f">Then you can merge your branch into the master:</p><div id="a521"><pre>git <span class="hljs-keyword">merge</span> newBranch</pre></div><figure id="29d9"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*1H1xrxl_iodsH5d0_ouobA.png"><figcaption></figcaption></figure><p id="4670">Then finally, you can push up to the remote repository:</p><div id="ff0f"><pre>git <span class="hljs-built_in">push</span> <span class="hljs-built_in">origin</span> master</pre></div><h2 id="1894">Deleting Branches</h2><p id="a9f1">Once we have pushed our changes from the branch to the master, we can delete our branch local:</p><div id="f6b4"><pre><span class="hljs-attribute">git branch -d newBranch</span></pre></div><figure id="5239"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*GL9R54Hggl7k5lhheVC-sA.png"><figcaption></figcaption></figure><p id="6296">We can also delete the branch from the remote repository:</p><div id="d5f0"><pre>git <span class="hljs-built_in">push</span> <span class="hljs-built_in">origin</span> --<span class="hljs-built_in">delete</span> newBranch</pre></div><p id="e69e">We can check the branches left by:</p><div id="dce2"><pre>git branch -<span class="hljs-selector-tag">a</span></pre></div><h1 id="c468">Putting it all together</h1><ol><li>Create a new branch to work on a new feature</li></ol><div id="dc8e"><pre>git branch <span class="hljs-keyword">new</span><span class="hljs-type">Feature</span> git checkout <span class="hljs-keyword">new</span><span class="hljs-type">Feature</span></pre></div><p id="c2cc">2. Work on code for the new feature</p><p id="6002">3. Check modifications we have made, add to staging directory and commit locally</p><div id="c239"><pre>git status git <span class="hljs-keyword">add</span> -A git <span class="hljs-keyword">commit</span> -m "New Features"</pre></div><p id="e161">4. Push to remote repository</p><div id="9203"><pre>git <span class="hljs-built_in">push</span> -u <span class="hljs-built_in">origin</span> newFeature</pre></div><p id="cb22">5. Once everything is ok, merge to local master</p><div id="8bd2"><pre>git checkout <span class="hljs-keyword">master</span> <span class="hljs-title">git</span> pull origin <span class="hljs-keyword">master</span> <span class="hljs-title">git</span> merge newFeature</pre></div><p id="6783">6. Push to remote repository</p><div id="9c76"><pre>git <span class="hljs-built_in">push</span> <span class="hljs-built_in">origin</span> master</pre></div><p id="365c">All done!</p><figure id="ab02"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/0*-1Uw8sSzic2-JuJc.png"><figcaption></figcaption></figure></article></body>

Git basics: a step-by-step tutorial

A gentle beginner’s tutorial on how to use Git

Photo by Yancy Min on Unsplash

What is Git?

Git is distributed version control system. It allows for multiple programmers to work on the same project.

If you intend to work in data science, quantitative analysis, or any role that requires programmers or coders collaborating, you will probably have to cross paths with it. At first Git can be quite intimidating to learn. It can also be quite confusing. This gentle tutorial will hopefully alleviate you from some of these fears.

Setting up Git

First, you can download Git from the website below.

Launch Git bash after installing it. Firstly, check the version of Git you have. Mine is shown below.

git --version

Set local configuration variables

git config --global user.name "Name"
git config --global user.email "[email protected]"
git config --list

At any time you can ask for help by typing the --help command in Git bash. For instance, if you wanted help on the config verb, you could type the following:

git config --help

Initialize a repository from existing code. Here I have a simple folder where I have a Python script called main.py and a few other folders. Yours obviously would be different. Let’s track this generic project with Git.

A sample code repository before git init

To initialize, we type the command:

git init
A sample code repository after git init

You will realize that the git init command has created a .git file in the directory.

If for some reason you want to remove from the code repository, you can type:

rm -rf .git

This will remove the .git file from the directory. Of course, don’t do this just yet. We have a lot more to do!

Before we commit to anything we can check the status of our project.

git status

As you can see, there are no commits yet. This is because we haven’t committed anything yet!

Now say the .idea file and userConfigs are files/folders that you might not want to track changes on. You can use a simple “gitignore” text file list all the files you want Git to ignore. You can do this by:

touch .gitignore

And then go and edit that file. For instance, I did this in notepad.

Now if you run git status again, you will notice that the .idea and userConfigs are not longer there anymore. Great!

Some Git Theory

There are three states in Git that we need to know about.

  1. Working Directory
  2. Staging Area — this is where we organize what we want to be committed to the repository
  3. Repository — this is our local repo

This visual will hopefully give you an idea of the relationship between each stage.

So far, we’ve simply been playing at the working directory stage. Next we need to know how to add files to the staging area, ready to be committed to the local repository.

Add files to the Staging Area

As you can see both files are currently just sitting in our working directory untracked.

We need to move them into the Staging Area.

Say we want to add main.py we can run

git add main.py

Then if we run git status again:

You can now see that main.py is now pushed into the staging area ready to be committed. Furthermore, you can see that .gitignore is still currently untracked.

Add all files to the staging area:

git add -A

Perfect! Now both files have been added to the staging area.

Removing files from the Staging Area

If you want to remove files from the staging area, you can use the git reset command. For instance, if you want to remove main.py from the staging area:

git reset main.py

If you want to remove all files from the staging area, you can simply type:

git reset

Commit from Staging Area to the Repository

Now that we have the files we need in the staging area, we are going to commit them to the repository.

We commit by typing:

git commit -m "type a message here"

Make sure your message makes sense for future reference. It is good to be precise on exactly what changes you have made.

This is our first commit.

Now when we run git status we find there is now nothing in the staging area.

We can also check the commits we have made by looking at the log.

git log

Sometimes Windows Explorer doesn’t show the .git folder. If that is the case for you, you need to set it so that you are able to see hidden files. You can do this by following the instructions in the link below.

So far, we have explored all three Git states on our local repository. Often we work in teams of more than one. Next we need to know how to copy a remote repository. This is what the next section is all about.

Cloning a Remote Repository

In real life, when you’re working in a team, you will need to clone an remote repository onto your local machine. You can do this with the following command:

git clone <url or directory>

If you’re simply starting out, you might not have a remote repository to clone from. So this is what we’re going to do instead.

You’re going to create another folder in your drive called clone/

And you’re going to clone from one directory to another. In my case:

This is because my original repo was in a folder called ‘sample’. Yours would be different of course.

Now you should be able to see a clone of your ‘not so remote’ repository.

If you type in the following command:

git remote -v

This will give you information on your local repository. Furthermore,

git branch -a

This will list all the branches, locally and remotely for this project.

Making a Change

Let us now make a change to a file in the clone repository. After making a change to a file, you can run:

git diff

This is list all the changes to the existing files you have made.

If you add a new file to the cloned repo, you can run the below to see new files and modified files.

git status

Now we need to add them to the staging area by running:

git add -A

Then we can run something like this to commit from the staging area:

git commit -m "newChange file added"

Ok, so now that we have made the changes locally, we’re going to push these changes up to the remote repository so others can see.

Pushing it Back up to the Remote Repository

Before we push things up to the remote repository, we need to pull from it to make sure that during the time we’ve been working on our own changes, there hasn’t been any changes in the remote repository that we cloned from.

git pull origin master

If things are good, we will get a reply that everything is already up to date. Now we can push.

git push origin master

Now you have pushed your changes back up to the remote repository!

Branching

Often when you’re working on a large project, different aspects of the project can be worked on it different branches. Different users can also work on different branches. Branches can later be merged.

To check the branches you currently have:

git branch

Most likely, you will only have the master branch.

To add a new branch you can simply type:

git branch newBranch

The one with the asterisk on is the one that you’re currently working in.

If you want to switch branches you can see the checkout verb.

git checkout newBranch

This will switch you to the new branch!

Now you can go ahead and make some changes to your files. You can see the changes you made by git status .

After making some changes, you can commit to your new branch

git add -A
git commit -m "changes to new branch ... blah"

After we have done that, we can push our new branch into our remote repository.

git push -u origin newBranch

Now if we run git branch -a , we can see all the branches (local and remote):

You can see that our local newBranch has been pushed up to the remote repository.

The idea of this is that others can review the changes you have made in the branch, run unit testing etc. Once everything is OK, you can merge the newBranch to the master. In the next section, we will be talking about merging branches.

Merge Branches

To merge branches, you first have to merge your branch locally with your local master. Then you can push this up to the remote repository.

So first, you need to be in your local master:

git checkout master
git pull origin master

Then you can merge your branch into the master:

git merge newBranch

Then finally, you can push up to the remote repository:

git push origin master

Deleting Branches

Once we have pushed our changes from the branch to the master, we can delete our branch local:

git branch -d newBranch

We can also delete the branch from the remote repository:

git push origin --delete newBranch

We can check the branches left by:

git branch -a

Putting it all together

  1. Create a new branch to work on a new feature
git branch newFeature
git checkout newFeature

2. Work on code for the new feature

3. Check modifications we have made, add to staging directory and commit locally

git status
git add -A
git commit -m "New Features"

4. Push to remote repository

git push -u origin newFeature

5. Once everything is ok, merge to local master

git checkout master
git pull origin master
git merge newFeature

6. Push to remote repository

git push origin master

All done!

Git
Programming
Data Science
Github
Python
Recommended from ReadMedium