An Introduction to Git and GitHub - Part 2

·

10 min read

An Introduction to Git and GitHub - Part 2

Table of Content

  • Basic Operations in Git
  • Branching and Merging
  • Undoing Changes
  • Inspect Changes
  • Conclusion

In the previous part, we learned about general operations in Git and GitHub. In this part, we learn advanced commands in git.

Basic Operations in Git

  • Add new file in Git

    To add a new file in the git repository use the touch command to create a new one.
touch new_file.txt
Touching new_file.txt

Then use the git status command to check repository status.

git status
C:\Users\HP\Desktop\areej>git status
On branch main
Your branch is ahead of 'origin/main' by 3 commits.
  (use "git push" to publish your local commits)

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

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

After creating a new file add the file to git and commit changes made to the repo.

git add new_file.txt
git commit -m "new file added"

The output will look like the following.

[main 1ee86a1] new file added
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 new_file.txt

After committing files to use check the status of the repo.

git status
On branch main
Your branch is ahead of 'origin/main' by 4 commits.
  (use "git push" to publish your local commits)

nothing to commit, working tree clean
  • Edit File / Restore Changes in Repository

If we make any changes to any file that needs to be added and committed to the git repository again. For example, in the previous step, I create a new file "new_file.txt" let's edit the file by writing some content and add to the repo again. Use the following command to write in a file:

echo "This is a text file. ">> new_file.txt

Use the status command, and the result will look like the following:

git status

On branch main
Your branch is ahead of 'origin/main' by 4 commits.
  (use "git push" to publish your local commits)

Changes are not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in the working directory)
        modified:   new_file.txt

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

Now add and commit the changes to the repository.

git add new_file.txt
git commit -m "edited file"

The output will look like the following:

[main 95a78b0] edited file
 1 file changed, 3 insertions(+)
  • Delete File in Git Repository

    To delete a file from the git repository using the 'git rm' command. Use the following code to delete files.
git rm new_file.txt
git commit -m "deleted"

The output will look like the following:

rm 'new_file.txt'

C:\user>git commit -m "deleted"
[main f7db923] deleted
 1 file changed, 3 deletions(-)
 delete mode 100644 new_file.txt
  • Rename a File in Git Repository

    To rename a file in a git repo, use the 'git mv' command. Use the following code to rename files.
git mv new.txt text.txt

git status
On branch main
Your branch is ahead of 'origin/main' by 6 commits.
  (use "git push" to publish your local commits)

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        renamed:    new.txt -> text.txt

Commit changes to repo:

git commit -m "rename"
[main ed162a9] rename
 1 file changed, 0 insertions(+), 0 deletions(-)
 rename new.txt => text.txt (100%)
  • Ignore Files in Git

    To ignore specific files in git, write files/ directories names to be ignored in a text file. These files may be log files, temporary files, hidden files, personal files, etc. Git normally checks gitignore patterns from multiple sources, with the following order of precedence: First, create a .gitignore file inside the main directory, and then add the file you don't need to commit or use. In my case, I create three files and added them to the .gitignore file.
touch gitignore
echo *.txt >> .gitignore

echo *.txt added all txt file names or simply use file names instead in the .gitignore file. If you wanna add a directory use format like directory/filename to add the whole directory in the .gitignore file. After this add and commit files to the repository. To check the files you added in the .gitignore file use the following command.

git o-files -i --exclude-standard

The result may look like the following:

file.txt.txt
file1.txt.txt
file2.txt.txt
git.txt

Undoing Changes

  • Git Checkout

    Git checkout command is used to create branches and switch between branches in a repository.
  • Create a new Branch
git checkout -b new_branch
Switched to a new branch 'new_branch'
  • Switch between branches

Use the checkout keyword to switch between different branches.

git checkout branch1
Switched to branch 'branch1'

git branch
* branch1
  main
  • Git Chery Pick

    Git Chery Pick is used to apply some commits from one branch to another branch. Git cherry-picking is helpful to apply the changes that are accidentally made in the wrong branch. For Example:
git add new.txt

git commit -m "new"
On branch new_branch
nothing to commit, working tree clean

I accidentally commit a file in branch1 so, to make all the changes to branch1 into the main branch, we will use the git pull, but for this particular commit.

git log


commit a1fc6f35e8be7ded83a1217125a075fe1dc7c929  (HEAD -> main)
Author: UserName <Email>
Date:   Thur Apr 28 07:41:47 2022 +0500

    branch

commit 0683323a6d0406c996ce4f78a131e6aca1ee4b14 (branch1)
Author: UserName <Email>
Date:   Thur Apr 28 03:41:50 2022 +0500

    ignored files

From the above code, I copied the merging commit that is needed to revert and run the below Use the git revert command to revert the changes.

git revert a1fc6f35e8be7ded83a1217125a075fe1dc7c929
This reverts commit a1fc6f35e8be7ded83a1217125a075fe1dc7c929.

# On branch main
# Changes to be committed:
#       deleted:    new.txt
Change the files

Branching and Merging

  • Git Branch and Merge

    Git branch is defined as the pointer to the changes we made to a repository. A git branch represents an independent line of development. This is mostly available in version control systems. A Git project can have more than one branch. When adding new changes or fixing a bug, you need to create a new branch to summarize your changes. So, it is complex to merge the unstable code with the main code base and also facilitates you to clean up your future history before merging with the main branch.

    The git branch command allows you to create, list, rename and delete branches. Use the following commands:

  • Check Branch status

    On entering the command, it will look like the following:

git branch
* master
  • Create Branch
git branch feature
git branch

The output will look like the following:

feature
* master
  • Rename Branch

    Let's change the branch master name to another one.

git branch -m master main
git branch

The output will look like the following:

feature
* main
  • Working on different branches
    To work on different branches, we will make changes to branches by adding files. Create a new file and commit the file to the repo and move from the current branch to another. Use the following commands:
touch m1 m2 m3

git add .
git commit -m "main branch"

In my case, I created an m1 file then add and commit it as "m1" and the same as m2 and m3 and commit them as "m2" and "m3". This will create a chain of commits in the log. The result may look like the following:

touch m1
git add .
git commit -m "m1"
[master (root-commit) 0fcf9a0] m1
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 master/m1

touch m2
git add .
git commit -m "m2"
[master 7f4a360] m2
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 master/m2

touch m3
git add .
git commit -m "m3"
[master 784986c] m3
 create mode 100644 master/m3

This will create a file like m1(root)->m2->m3(head) in the main branch.

Now use the git checkout command to change the branch. In the feature branch create a file f1 and check the log file it will be linked to the previous m3 file from the main branch.

git checkout feature
Switched to branch 'feature'
git log

This will show the commits in a chain in the feature branch

commit fbc34e0f2dc91b4d522d3207e6395d0edce74ed0 (HEAD -> feature)
    f1

commit 784986c6f70ece1925702a9d5b991a46d50a09e7 (main)
    m3

commit 7f4a360dfccbd842ba2f08aaca22a4d92d02ea58
    m2

If we go back to the main branch there will be no f1 file and if we create a new file in the branch that will not be shown in the feature branch.

  • Merge Branches
    Use the git merge command to merge two branches. By using the merge command we will tell git now which branch we want to merge our main branch or in our main branch is the feature branch like that. If we use the merge branch we could only have one last commit which combines all the changes of both branches of the latest commits of both branches but you would also merge all the other commits of the feature branch into the main branch by this git will allow us to track how our project resolves so what different commits which things we implemented which changes we have made afterward and soon.
git merge feature

The output will look like the following:

Merge made by the 'ort' strategy.
 feature/f1 | 0
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 feature/f1

As I created m4 file after creating f1 so its make a merge f1 to m3 file. Use git log to check how the merge happens between branches The output looks like the following.

commit 3b640b9f8a9bbd690517a402744ebdf13e55c0c9 (HEAD -> main)
Merge: 7d0027a fbc34e0

Merge branch 'feature'

commit 7d0027ad4d2e29cf4aa03616009226daa344e10f
    m4

commit fbc34e0f2dc91b4d522d3207e6395d0edce74ed0 (feature)
    f1

commit 784986c6f70ece1925702a9d5b991a46d50a09e7
    m3

commit 7f4a360dfccbd842ba2f08aaca22a4d92d02ea58
    m2

commit 0fcf9a01bf3bc084ee13b75345f8f8c253772ae4
    m1
  • Git Squash

    Squash is a keyword that simply allowed us to summarize all the different commits so all the change we had in the feature branch in the last commit and then merges this last commit with the latest commit in the main branch. To do this use squash instead of merge.
git merge --squash feature
git add .
git commit -m "main and feature merge"

The output looks like the following.

commit fbc34e0f2dc91b4d522d3207e6395d0edce74ed0 (HEAD -> main)
    main and feature

commit 784986c6f70ece1925702a9d5b991a46d50a09e7 
    m3

commit 7f4a360dfccbd842ba2f08aaca22a4d92d02ea58
    m2
****
  • Git Rebase

Rebasing is a process to reapply commits on top of another base trip. It is used to apply a sequence of commits from distinct branches into a final commit. It is an alternative to the git merge command. It is a linear process of merging. By using git rebase git will check both branches and merge the recently committed files. In my case I created a file m4 and commit it after creating feature branch so to merge or update m4 as root to feature branch we will use rebase.

git rebase main

The output looks like the following.

commit 8d4f798b1b89140f43562df33025308001f61608 (HEAD -> feature)
    f2

commit b223a20ceb1449ef3fb6d9499531f686349b403f
    f1

commit 32f3001dbc4bcb70c350b8e02b2456a9393d52ea (master)
    m4

commit 84d3680603d868865bf5053d4a83d30d16a93023
    m3

Inspect Changes

  • Git Status

    The git status command is used to display the state of the repository and staging area. It allows us to see the tracked, untracked files and changes. This command will not show any commit records or information.
git status

When there is a need to commit changes.

On branch main
Your branch is ahead of 'origin/main' by 4 commits.
  (use "git push" to publish your local commits)

Changes are not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in the working directory)
        modified:   new_file.txt

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

When all the changes are committed.

On branch master
nothing to commit, working tree clean
  • Git Diff

    Git diff is used for changes between commits, files not staged, staged files but not committed, track the changes after committing and track changes between two branches/ commits.

  • Git Log

    Git Log is used to keep the history of changes made to the repository. This helps us to retrieve data files, bugs, and commits. By using log files we keep track of changes to any file.

git log
commit 7b01428f02cf0baede6c8661d9e7b3e42946e970 (HEAD -> main)
Author: UserName <Email>
Date:   Thur Apr 28 07:41:47 2022 +0500

    branch

commit 0683323a6d0406c996ce4f78a131e6aca1ee4b14 (branch1)
Author: UserName <Email>
Date:   Thur Apr 28 03:41:50 2022 +0500

    ignored files

Conclusion

This article covered the basic commands that'll help get you started using Git. We also started learning how to use GitHub. By the end of this article, I hope you are familiar with the basic operations and commands of Git.