DEV Community

Dave
Dave

Posted on

How I use git and gitHub to store folders in the cloud

I've been using git and gitHub on my Linux laptop for some time now. Though I like what each technology does, the process I've hunted for is difficult to find. There is much information about git's use for version control with and without gitHub. However, I'm not trying to do version control. I just want to keep individual folders synced with a repository of the same (similar) name on gitHub. Trying to understand the complexity and quantity of commands has been really daunting. After working with both tools and researching on my own, I've come up with a minimal set of git commands that work well with gitHub. These commands allow me to keep one folder synchronized to one gitHub repository as a backup location.

git is in most of the major Linux distribution repositories. I'm only used to apt-get (aptitude) on Ubuntu/Debian. If your distribution is different from that, look up how to add programs/apps/utilities in your version of Linux.

For Ubuntu/Debian users it would be:

$>sudo apt-get install git

After install is complete, navigate to a folder that you wish to sync with gitHub. Don't worry yet if you haven't set up a gitHub account. We'll get to that part in a bit. I'm going to do this as I just did today from my ncurses folder.

Before we begin with the steps to synchronize a folder with itself and with gitHub, there are two configuration commands you need to run. These are important and only have to be done once. They can be done from any folder since they are global to your git system.

$>git config --global user.email youremailaddress.com

$>git config --global user.name "Type your name here"

Now, cd (change directory) to the folder that you wish to synchronize with gitHub. Our first task is to build the local repository. I created an ncurses folder to house all the C and Perl files I'm writing to help me learn to use the NCurses library/module for C and Perl.

That first task is to init your git (create the repository system):

$ncurses>git init

This will create a new git repository in your folder. To check that it did, do an ls -a. In each folder that you init your git, you'll find a .git sub-folder.

The next step is to add all the files in your folder. There are probably hundreds of ways to do this to pick 'just the ones' you want, but it's easiest if you maintain only files in the folder that you want synchronized; nothing extraneous. That way, all you'll really need is:

$ncurses>git add *

This adds all files (and sub-folders) to your branch. Branches are important in git, but for us, each folder has one master branch and we are going to keep it that way. To see that they've been added, run the following:

$ncurses>git status

Learn this one well. It is the most used git command, in my experience. git status will tell you if your folder is up to date with your git repo. It will show you (in red, possibly) when you've made changes, and (in green, possibly) when you've done a git add * and are ready to commit your git with the following:

$ncurses>git commit -m "My first commit with date, etc."

If at all possible, do your commits like the above as it is simplest. You can and should change the message to describe the commit. git can call up your favorite editor and fill out a message, but that seems more complicated, in my view.

Up until now, we've only worked on the local repository inside your folder. If you run git status again, you should see an "up to date" message from git. Now, it's time to turn our attention to gitHub.

Go to gitHub.com and sign up for an account if you haven't already. After you're logged in, you'll want to create a gitHub repository. The instructions for this can be googled. Set up a new repo, add the README.md file, set a LICENSE file, and do whatever else seems appropriate to your use. When you click that NEW button, gitHub will require a reasonably unique name for your repository and it is the most important item. It won't let you get further without it. I would also add, make it memorable and link-able to your local folder. Mine all generally match the folder name or project I'm working on. Also, choose whether you want your gitHub repo private or public. It makes no difference on use, so the choice is yours.

Once your gitHub repo is created and you're ready to continue, look at the middle of the repo page, just before you see your file list. It'll look something like this:

Github Clone Button Location

That green button on the right is the one we want. When you click that button (don't worry, we won't be making clones), it drops down a pop-up that gives you the direct URL for your gitHub repo. This button is in the same place on every repo you create.

The next steps will be done in our chosen folder. I'm still in my ncurses folder. First, we'll add a remote branch for our local git repo. Go to your gitHub repo, click the 'Clone' button and copy all of the URL it shows you. Then type the following command and paste your URL on the end:

$ncurses>git remote add ncurses https://github.com/githubusr/yurrepo.git

I put ncurses as it is the folder name I'm working with, but this could be anything you wished to call it. The default is either origin or master, I can't remember (exactly why I don't use the defaults). By always using the folder name, you'll easily remember some of the commands you need below.

There won't be any response other than a new prompt when you do this. If git replies with anything, it will likely be an error. By the way, you need to be connected to the Internet when doing these gitHub related commands.

To check to be sure it worked, type the following to see the URL you pasted into git:

git remote get-url --all ncurses

Replace ncurses with your folder's name used in the git remote command.

Because we added a README.md file when we created our gitHub repo, git is going to have problems syncing up with our files in our folder. In addition, if you are trying to sync up the gitHub repo with a new git repo (you got a new laptop, PC, drive, whatever), git will also have a problem. Before we sync our local repo with gitHub, we pull down everything in the remote (gitHub) repo and merge it with our local repo with the following command:

$ncurses>git pull ncurses master --allow-unrelated-histories

We set up the ncurses remote name above with git remote. The master branch is the default branch name on all of our gitHub repos, but as long we we are in the appropriately set up folder/repo, git will know what to do (mostly because we put ncurses as the remote name). The --allow-unrelated-histories key is highly important. It will get everything out of the remote (gitHub) repo and merge it with your local repo. Do an ls after running this command and you should see a README.md file added to your folder. This command will open a new line asking for your gitHub user name, followed by another new line asking for your gitHub password before it completes the command.

We'll only use git pull at special times such as setting up a new gitHub repo or getting things back down from gitHub after something major happens (new folder, new drive, new PC, etc.).

On a regular basis, after we've done a good git commit, we'll use the following command to push changes in our commit up to the gitHub repository:

$ncurses>git push Cfiles master

Again, you'll be required to enter your gitHub user and password in order for the command to complete. When this step finishes, you should see all of your folder's files (in this case, my ncurses folder) in the gitHub repo you set up.

If you've gotten both your local git repository working and push-ed everything up to a gitHub repository, then on a daily, weekly, whichever, basis, you need the following commands:

git status
git add *
git commit -m "with your message"
git push yourlocalname master

There is one additional command you will need as time goes along:

git rm filename
or:
git rm {file1,file2,file3,...}

If you've deleted files from your folder, git sees this. It will show you the files you've deleted, but still exist in the repo with lines like :deleted filename when you do a git status. git rm will remove these from the repo and clean up your commits. I've also found that it treats files you move to a sub-folder the same way. It considers them deleted, yet, when you git rm the deleted file, it straightens up the branch by now showing where you had moved the file (at least it did, today).

gitHub and git are two robust tools for keeping your work backed up and well ordered. While their use as version control tools has many facets I didn't cover today, these can be used as a minimum needed to get oneself started. By keeping things to a single folder per gitHub repo when using git and gitHub, it also simplifies the process, I believe. I've been using this method for about a month now and have had good success with it. I would rate it higher than Google Drive or Dropbox because of it's orderly nature. I hope you will be successful as well. Let me know if it does, or ask questions if needed.

Top comments (2)

Collapse
 
padakipavan profile image
padaki-pavan

Take a look at restic. It has everything you described plus a lot more features built-in.

Collapse
 
tardisgallifrey profile image
Dave

I like that. It looks like a cool app to use as well. However, I put a lot of effort into getting git set up, I think I'll stick with it for now. Thanks for the tip.