Git introduction & Cli commands for beginners

Git introduction & Cli commands for beginners

Introduction:

Git is a version control system, it is free and open source, git was developed back in 2005 by Linus Torvalds. Yes the founder of Linux. It was developed as a distributed version control system. Git is powerful and very useful tool when it comes to coding projects. Git enables you to share your work with others for reviews and if you are stuck at some place you can get help. Many people use git as just a local version control system but its real power comes in distributed architecture where many people from different locations can work on the project and collaborate.

Difference between Git Hub and Git Lab:

Its important to know that git is not like github or gitlab. Github and gitlab are web-based git repositories. Both help to share your local files, manage the code plus do issue tracking. Github is claimed to be the largest source code hosting platform. Github projects can be made public which means that publicly shared codes are open to everyone. Github allows private project option as well but the free plan has limited collaborators allowed. On top of it gitlab also give CICD/DevOps features to developers.

Working with Git:

Git might not come natively with the OS, for windows and Mac we need to download and install it from. In case of Linux, it might have it already. The command line version can be downloaded for practice and daily use. The GUI based Git clients are also available. We will be working with Git Cli in this tutorial.

Git Workflow:

·        Working directory    

·        Staging area   

·        Local Repository  

·        Remote Repository

Below are the steps to follow:

Step 1: Create account on GitHub server using your email credentials on this link

No alt text provided for this image

Step 2: Once you login, create your Repository on GitHub server. (Public/Private repo optional). When you press create repository on the next page you have to copy the link for accessing this repository remotely e.g. ssh/http.

No alt text provided for this image

Step 3: Download and install git on your local PC e.g. Windows/MAC/Linux etc with the default options. 

No alt text provided for this image

Step 4: Once Installed, open git shell and clone your remote (GitHub repository) on the local PC with the below command. (the path you need is on your github account page you need to copy it e.g. http/ssh)

No alt text provided for this image

Step 5: Create new file in your empty cloned repository and check the git status as below.

No alt text provided for this image

Step 6: Add the new file into staging area otherwise git will not track it.

No alt text provided for this image

Step 7: Once the file is added to the staging area, you can commit the file changes with comment.

No alt text provided for this image

Step 8: Now you can push your files with changes onto the remote repository (github server) that you created earlier. [git push (server path) (branch name)]

No alt text provided for this image

Step 9: New file will appear on your online repository now, your local and remote repositories are sync now.

No alt text provided for this image

Step 10: If you want to create new branch, follow the below steps. Use [git branch (branch name)] and [git checkout (branch name)] to create and move between branches.

No alt text provided for this image

Step 11: Add some comments on your “Hello.py” file in new-branch, add and commit changes then push the changes to online repo. Once these steps are done check the online status

No alt text provided for this image

Step 12: Now go to the main repository and check the difference between main (hello.py) and new-branch (hello.py) files. You will see the difference in red color as below. To merge changes in both branches into one, issue the merge command and then check the difference again. There should be no difference now as both files have been merged as can b

No alt text provided for this image
No alt text provided for this image

Step 13: If you want, you can delete the new-branch with the below command

No alt text provided for this image

Some Other Useful Commands:

  1. Git mv –f (source) (destination) [To Move a file]
  2. Git remote add (name) (url) [For git init make sure that you enter the information to find the remote location of server]
  3. git remote –v [find out which remote repository is configured] 
  4. Git remote rm (name) [To remove the remote tracking of repository use]

References:


To view or add a comment, sign in

More articles by Khurram Javed

Others also viewed

Explore content categories