Version control and Linux

Here’s an excerpt of what I have learnt from crio #learnbydoing challenge about version control and how to contribute to open source.

Linux commands

Linux is the best platform hands down, followed by mac (I’m not adding windows to the list, because it doesnt deserve it). If youre looking to develop stuff or work on a project Linux is the go to OS. In order to better know how to take advantage of the Linux system, you need atleast some understanding of file structures and a handful of basic commands.

know your location

To know where you are in your computer workspace use

pwd

This gives a directory structure which shows where you are.

in linux locations are separated by /

eg. /home/user/Documents

ls -a

this command lists all files within the current working folder.

change location

cd the_folder

this takes you to the the_folder directory

cd ..

this takes you to the previous folder in the structure

cd ~

takes you to the home directory

file/folder manipulation

In order to create a new folder

mkdir new_directory

creates a new folder named new_directory

cp old_location new_location

this command takes the file or folder from the first argument and places it in the second argument

mv old_location new_location

to remove a folder/file

rm -rf folder_name

rm stands for remove and the -r stands for recursive meaning everything within the folder will be deleted , -f means no prompts will be displayed before deletion

But be careful before using these commands because there is no undo button

file creation

There are a lot of ways to manipulate files and creating them

to create a new file use

touch newfile.name

there are a variety of command line text editors that can be used to change and edit files. The most popular ones are vim and nano

nano file.name

this opens file.name file and you can edit this file from the command line itself.

Grepping

Grep is a tool which helps in searching for key words and patterns in the files you specify. This is extremely useful if you constantly lookup words in large files.

grep yourword file1.name and file2.name

this command searches for yourword in file1 and file2. there are a lot of flags which are out of the scope of this blog but worth looking into

Git

Git is a version control system, which means multiple people can work on the same project and have multiple versions concurrently and integrate it to the main project.

This is extremely useful in large open source projects that are being contributed to by hundreads of people at any given time. How to contribute to open source is discussed later.

The best way is to use github and signup with it. signup here github

Now let’s start with the basics. First install git on your platform(preferably linux) with the command that suits your system

On linux

sudo apt-get install git

On windows

If youre on windows the best way if to get git desktop or github desktop and then using if from there. Another workaround is to get a linux subsystem running with instructions from this blog https://www.windowscentral.com/install-windows-subsystem-linux-windows-10

On mac

If you have xcode cli installed you can check if git is installed with

git --version

Or if you use homebrew just type

brew install git

With everything set up and ready, assign your identity on git with

to set your username

git config --global user.name "YOUR_USER_NAME"

to set your mail id

git config --global user.email "YOUR_MAIL_ID"

Starting your first project

The best way to start a project is going to github.com and creating a new project

now press on the download code button in green and copying the url.

In terminal open the location where you want your project to lay and type

git clone the_url_you_copied

This will create a new folder with the same name as your project name . move into this folder and youll find no files beacause you started a new project. Now add your project files to this folder.

Now you have added the files but git does not know which files to track and which not to. so in order to add the files to your tracking list

git add .

adds all files you have in your folder to the tracking list, to add just a file,

git add yourfile.name

The next step is to tell git that all files have been changed. You do this by

git commit -m "message"

This command commits (solidifies changes) all the changed files that have been tracked. -m is the message you give to your commit, which makes it easier for others working on the project know what the commit is about. (it makes it easier for you too). There is more than one way to do the same thing everywhere, you can research into this more.

Now we push the changes to our git space with

git push

now if you go to your github profile you will see your changes reflected in your repository (project folder).

Congrats, you have just created and committed your first change!

Checking out others projects

You can also clone and try out other people’s open projects. This is done the same way you do your git clone. In order to contribute a feature or a creative new idea to someone elses projects, you need to fork their repository. But do NOT push changes directly to their main branch(called master), but create a new branch and then push changes which if the original repo owner likes your changes will be pushed to the main repository.

And there you go, a brief intro to version control and the linux OS. Although we havent even scratched the surface of the glass, these should help you move out and get things done. Research into topics that interest you.

I learnt a lot from this micro experience at #crio #ibelieveindoing

Leave a comment