Practical Git Lesson — Part I

Git
Git is a free and open-source distributed version control system designed to handle everything from small to very large projects with speed and efficiency.
Git is easy to learn and has a tiny footprint with lightning-fast performance. It outclasses SCM tools like Subversion, CVS, Perforce, and ClearCase with features like cheap local branching, convenient staging areas, and multiple workflows.
Why Git?
- It is a Distributed Version Control System.
- Multiple users can keep different versions of their code.
- Easy to track and roll back if anything goes wrong.
- It allows changes from multiple people to be merged into one source.
How to install git?
cmd: sudo apt-get update
cmd: sudo apt-get install git
How to find the git version?
cmd: git — — version
How to set up the username and email for git?
cmd: git config — global user.name “mayur”
cmd: git config — global user.email “mayur@xyz.com”
How to create a repository?
cmd: git init sample_repo
It creates a sample_repo folder with git support.
How to check if the repository is created or not?
cmd: ls
cmd: cd sample_repo
The below command shows the hidden files in the git repo
cmd: ls -a
The above commands create a git repo from scratch. But if you have an existing project which is loitering around without git support then, you can move to the main folder of that project in the terminal and then type the command as
cmd: git init
Note: git init without the repository name takes the folder name as the repository name. We can check the .git file in the project folder after the git init command to find the repository name. // represents a comment. Don’t copy/paste the comments.
How to change the name of the project name of the git repository?
To change the project name of the git repo, just move the folder to a new folder as below (a new folder will be created automatically)
cmd: mv sample_repo/ git_basics_lessons
How to remove the git support from the project?
To delete the git support from a project, we can delete the .git file from the project folder
cmd: rm -r git_basics_lessons/.git
How to commit a file?
cmd: git init git_basics_lessons
The above command enables the git support for the git_basics_lessons folder
cmd: nano README
The above command creates a readme file with nano editor
cmd: git add README
The above command makes the git keep track of the changes in the file, it helps not to send all unnecessary files to the repository.
cmd: git commit -m ‘Added a README file’
-m refers to a message while committing the file. It will commit the changes locally.
Edit the existing ReadME & Commit again
cmd: nano README
actions: Edit the README file
actions: Save the README file
Commit changes in the README file
cmd: git commit -a -m “new feature statement added”
-a (includes all the changes)
Staging Area: It helps in selecting exactly what we need to commit
How to find the repository status?
cmd: git status
We won’t see any interesting messages in the status command unless we made any changes to the file without committing it or added new files without committing it.
Adding few files in a git repo
Let’s create two files with names file1 and file2, then find the status of the repo.
cmd: touch file1
cmd: touch file2
cmd: git status
The git status will show the two untracked files.
Add file1 that needs to be tracked
cmd: git add file1
cmd: git status
The git status will show us two messages, First, the file is added but didn’t get committed and second is the file2 is untracked.
cmd: git add file2
cmd: git commit -m “Added two files”
cmd: git status
Making changes to file1 and file2
cmd: nano file1
actions: Edit file1
actions: Save file1
cmd: nano file2
actions: Edit file2
actions: Save file2
Since we have already added the file in repo before, git sends the status that the files are modified but are not added for commit.
cmd: git status
Commit the modified file1 and file2
cmd: git add file1
cmd: git commit -m “changed file1”
cmd: git status
cmd: git add file2
cmd: git commit -m “changed file2”
cmd: git status
How to view the history of commits made?
Each commit is named with a unique hash value as follows ‘commit e279hf92h9823e39….’
cmd: git log
we can view what each commit was, the commit identifier. By giving partial identifier detail like e279, we can extract what was committed to the repo.
cmd: git checkout e279..
It will give us the details of the commit, like check out the status of the repo at that time of commit. And we can check out files, which we added during that commit or made any changes in the code during that commit.
cmd: cat file1
cmd: cat file2
To move back to our current status of the repo with all the changes that have been made, we can check out from the main branch (previously instead of the main, the master was used).
cmd: git checkout main // ‘main’ is the branch name of the repo
How to find the difference between the two different commits by using the commit identifier?
cmd: git diff gh887 hjk89
This will show the difference between the two commits. gh887 and hjk89 have committed identifiers as mentioned earlier.
In the second blog post, I’ve discussed about branching, merge and remote repository.
I hope you’ve enjoyed the practical guide on git. Thanks for reading. Enjoy the commits you make.
Originally published at https://mayurji.github.io.