Version Control part 1
Week 1 — Version control
Version control is a wonderful thing, this allows you to save multiple versions of any given piece of code. There is a multitude of reasons why we should use this in all our projects, just some of them being;
Encase we make any undesirable changes to our code and wish to revert back.
We can refer back to previous versions to see what has changed, whether it be a good or bad development.
We can use previous versions for trouble shooting.
The list could go on…
Git, what is that? Git is the most commonly used system in version control. Git can easily be used on windows and Unix based systems so no need to threat if your OS will be compatible.
Setting up your environment is relatively straight forward, you simply download git from https://git-scm.com/ and ensure its installed correctly using “git — version” you should receive back the version of git installed.
Now a few basic configurations, git config — global user.name “your name” git config — global user.email “your email” These two commands configure your systems git profile.
In addition, some commands change the settings: “git config — global core.autocrlf input git config — global core.safecrlf true”.
Making a git repository
The first step to this is to create or locate the desired directory “mkdir gitDir”, then you’ll need to navigate to the directory “cd chosenDir”. Now to add a file, fore example we could could create a new file with some text “echo “Git is Awesome” > gitText”. Now here we have a new file named gitText, we now initialize using git init “git init” then add the file we just created as follows “git add gitText”. Now all that’s left to do is to commit the file to our repository, you do this as follows “git commit -m “First commit””(-m “” is used to submit a message with the commit, for example “Version 2 of my project”).
Note; When I first attempted this I encountered the issue “fatal crlf would be replaced by lf” To resolve this issue requires a quick configuration with the following “git config core.autocrlf false”.
“git init” This function is used to initialize a repository, it is important you do this in the desired directory. If you accidentally initialize git in the wrong location use “rm -rf .git”, this removes the hidden .git folder which contains your version controlled files.
“git add” We use this to add a file to a staging area, the staging area is where we keep the files we want to be under version control. Anything not added to this staging area will not be tracked, we wouldn’t want to add files such as temp files and so forth so we can be selective.
“git commit -m “message”” The git commit command will commit the files added to the staging area, “-m” is used to add a message to the commit. If the commit is successful you’ll receive a message back which will specify a root-commit message, code and number of files changed/added.