Introduction to Version Control
Lets go through little about the Git Version Control

Introduction to Version Control

Git, What is it, and why we should care? - That's a good question if you are new to Git or coding world then, let me tell you this, you must learn about Git and I will tell you why.

Why - Version Control allows you to store your source code somewhere safe while also keeping older versions of your code, in case if you want to see the changes or want to simply go back in time. I don't mean it in literal way don't be silly ??. Version control is a system that records changes to a file or set of files over time so that you can recall specific versions later. Version Control allows you to share your code with other developers and have collaborative project by allowing to work together on a same project with as few conflicting changes as possible of course.

There are many other ways to go to control the versions of your source code, but if you don't wanna be viewed as with curiosity or distrust and even pity you must know about Git. So Git is the standard tool for version control in our modern world. ??Actually there are some big companies that use some other ways but come on people.

So is it easy to learn Git? - Some people say its hard at first but gets easy after using it for awhile, but I have to be honest that it is not. Here is the proof: https://git-man-page-generator.lokaltog.net This is the Git documentation its is so bad that a lot of people agree. It will most certainly take up a lot of brain power to learn Git but don't fret you are not alone. The only way to learn and feel confident about using Git is by using it. I will break this Introduction to Git into small tutorials like articles by going through practical and pragmatic ways so that you can build up your skills slowly. I will learn more down the road with you as well so if there is any mistakes you encounter, please let me know in comments below.

Git Most operations in Git need only local files and resources to operate — generally no information is needed from another computer on your network. Git doesn’t need to go out to the server to get the history and display it for you — it simply reads it directly from your local database. This means you see the project history almost instantly. If you want to see the changes introduced between the current version of a file and the file a month ago, Git can look up the file a month ago and do a local difference calculation, instead of having to either ask a remote server to do it or pull an older version of the file from the remote server to do it locally.

Best way to master Git is to understand how it works and why it works that way. But first lets go through so basic commands. I will be using Mac through out the tutorial (Sorry Windows users ??). You can also use Linux. Lets create a directory testGit on your Desktop, before we start using git lets make sure its installed. If not please install.

# Mac
# Step 1 install Homebrew

Go to link ->  https://brew.sh/

# Copy & paste the following into the terminal window and hit Return
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
brew doctor

# Step 2 – Install Git
brew install git

# Linux 
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install git

After installation we have to configure git to be able to use it. Lets run these commands below.

git config --global user.name "AliGorithm" // use your name 
git config --global user.email "[email protected]" // please use your email

You can also ask Git to have different output color. Lets run this command below:

git config --global color.ui true 

Now lets create a Git repository

There are several ways of setting up a Git repository - the name for a project being managed by Git but in most of the cases we use only two of the cases.

1. Create a new repository locally 2. Create a new repository in a Git hosting site like GitHub or BitBucket

How to create a repository locally? when 'git init' command is run on a terminal it will initiate the git local repository make sure to be in the right directory when you run the command.

$ pwd // to see where you currently are
$ git init

// Output similar to this
Initialized empty Git repository in
/Users/aligorithm/Desktop/testGit/.git/

This is actually all there is to it on how to create local repository, Git created a repository called .git inside the current directory, where it will store all the information about the repository. WARNING: If you delete that directory and you don't have a backup, you will loose all the repository information, all change histories are gone but not your current code.

Adding and committing

Git tracks contents, if you are coder most of the time your code. To demonstrate lets create a content. Using the vi or any text editor of your preference please create a text file inside the "testGit" directory and the line of code or any text.

$ vi dummyFile.txt

and add the text 
print("Lets see if i like Git")

NOTE: We are not writing some meaningful code here, and also that when we delete or create new files I will be assuming you are in the testGit directory.

After we safe the file, lets try run the next command,

$ git status // This will show us this output 

On branch master

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)

    dummy.txt

nothing added to commit but untracked files present (use "git add" to track)

Our file is listed as untracked, that means it is inside the repository directory but Git has no clue what it is as it is not added to track. But as you might have notice Git gives you guidelines on how to add a file to track 'git add'.

In our case we have one file we need to add to track changes to do that:

$ git add dummy.txt

// OR we can also say
$ git add . // to add all the changes in the current direcotory 

// after one of those commands ran lets check the status
$ git status

// Output
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

    new file:   dummy.txt


Our file dummy.txt now is moved from untracked to tracked list of files in our repository. Git is aware of the file. This means we have queued up the changes on to our repository, but we have not told Git to act on those changes. This is known as 'staging' area. Very useful to prepare list of changes all at once. When you are ready you can commit all the changes, when Git saves all the changes to the disk.

Now lets try to run the following commands:

$ git commit // this will launch the text editor where you can add a message that describes your changes.

//Those messages should be meaningful so team memebers that you are working with
// or future yourself should be able to understand the message

// But there is a faster way of adding messages
// run the command
$ git commit -m 'The initial commit' // and enter

// Output
[master (root-commit) 91df765] Initial commit
 1 file changed, 1 insertion(+)
 create mode 100644 dummy.txt  

If you run the git status now, you will see a different output.

// Output

On branch master
nothing to commit, working directory clean    

It means that all the changes are written to disk and no other changes are detected. Now to check if it is really that great at tracking changes lets change the message in our file dummy.txt

$ vi dummy.txt
//Add change the message 
print("Lets Change the message")
//save and quit 

You probably have already guessed what command now we should run.

$ git status 

//Output
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

    modified:   dummy.txt

no changes added to commit (use "git add" and/or "git commit -a")

The file is being tracked but it has been modified so that its a different from the version Git has saved earlier. We have to run git add . again and commit the change with the different message git commit -m 'This is a change' Lets run them.

Summary: In this short introduction to Git, I have covered a little about what it is Version Control/Git, we have covered a little about basic commands what is tracking and saving different versions of the file. In the next coming short tutorials I will cover Branches and merges. Until then try playing around by creating new files and tracking changes in the repository. Thank you for your time.

If this post was helpful, please like ?? and share, share your solutions as well in the comments below!

AliGorithm ??

def letsStayThirstyForMoreKnowledge(): 
     print("Happy Coding to you All!")

#Git #VersionConrol #Algorithm #Cloud #Docker #Containers #AliGorithm #DockerWhale #LinuxAcademy #AWS #GoogleClouds

RAJANNA S C

Engineer, Cloud Operations (AWS)

6 年

Could you please show how to add files from local repo or system to git hub

回复

要查看或添加评论,请登录

Ali Abdukarim的更多文章

  • K3s: The Lightweight Kubernetes Champion

    K3s: The Lightweight Kubernetes Champion

    Kubernetes has indisputably transformed the landscape of container orchestration, offering unparalleled scalability and…

  • Terraform and Terragrunt: Leveling Up Your Infrastructure as Code

    Terraform and Terragrunt: Leveling Up Your Infrastructure as Code

    Infrastructure as Code (IaC) has transformed how we provision and manage cloud resources. Terraform, from HashiCorp, is…

    1 条评论
  • A little intro about Containers: Part 2

    A little intro about Containers: Part 2

    Fundamental Docker Concepts Lets go through the fundamental parts of Docker. Docker provides the ability to package and…

    3 条评论
  • Algorithms: #01 Bubble Sort in Python

    Algorithms: #01 Bubble Sort in Python

    Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in…

  • Algorithms: #01 Bubble Sort in Swift

    Algorithms: #01 Bubble Sort in Swift

    Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in…

  • A little intro about Containers

    A little intro about Containers

    But first what is Docker? - One thing for sure it is getting a lot of attention from developers and system admins…

    10 条评论

社区洞察

其他会员也浏览了