DEV Community

Cover image for "Supercharge Your Github Journey: Must-Know Command Line Commands for Beginners"
Kannan
Kannan

Posted on

"Supercharge Your Github Journey: Must-Know Command Line Commands for Beginners"

Introduction

GitHub has become an indispensable platform for developers. It allows collaboration, version control, and seamless code integration. To fully leverage GitHub's potential, it's crucial to understand the role of command line commands in enhancing your workflows.

Getting Started with GitHub

Setting up a GitHub account

Visit github.com and sign up for a free account.

Complete the necessary information, including your username and email address.

Verify your email to activate your account.

Understanding repositories and commits

Repositories are containers for project code. They serve as a central hub for file management and collaboration.

Commits are snapshots of your code at a specific point in time. They represent changes made to the repository.

Introduction to Command Line Interface (CLI)

The Command Line Interface (CLI) plays a fundamental role in navigating and controlling GitHub workflows. It allows developers to interact with GitHub directly from their terminal.

Installing and configuring a CLI

Choose a CLI tool, such as Git or GitHub CLI, and install it on your machine.

Configure the CLI by setting your username and email address for proper identification.

Basic Command Line Commands

To navigate and manipulate files and directories efficiently, it's crucial to familiarize yourself with basic command line commands.

  • Navigating directories and files

Use the "cd" command to change directories.

kannan@kannan-PC:~$ cd newrepo
kannan@kannan-PC:~/newrepo$
Enter fullscreen mode Exit fullscreen mode

Utilize the "ls" command to list files and directories within the current directory.

kannan@kannan-PC:~/newrepo$ ls
1.php  2.py   4.php        file3.txt   newfile3.txt  README.md
1.py   3.php  4.py         header.php  newfile.txt   sample.txt
2.php  3.py   example.txt  index.html
Enter fullscreen mode Exit fullscreen mode
  • Creating and deleting files and directories Create a new file using the "touch" "vim" command.
kannan@kannan-PC:~/newrepo$ touch testfile.txt

Enter fullscreen mode Exit fullscreen mode

Remove files or directories with the "rm" command.

kannan@kannan-PC:~/newrepo$ rm testfile.txt 
Enter fullscreen mode Exit fullscreen mode
  • Modifying file permissions

Adjust file permissions using the "chmod" command.

Grant or revoke read, write, or execute permissions to authorized users.

kannan@kannan-PC:~/newrepo$ git add --chmod=+x testfile.txt

Enter fullscreen mode Exit fullscreen mode
  • Initializing a Git

A Git repo is essential for version control. Initializing a repository allows you to track changes and collaborate effectively.

sudo apt update
sudo apt install git
git --version
Enter fullscreen mode Exit fullscreen mode
  • Initializing a local repository

Use the "git init" command to set up a repository in your current directory.

This creates a hidden ".git" folder that stores repository information.

git init
Enter fullscreen mode Exit fullscreen mode
  • Creating a repository

Image description

  • Cloning an Git repository

Clone an repository using the "git clone" command followed by the repository's URL.

This creates a local copy of the remote repository on your machine.

kannan@kannan-PC:~$ git clone git@github.com:kannanb95/samplerepo.git
Cloning into 'samplerepo'...
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Receiving objects: 100% (3/3), done.
Enter fullscreen mode Exit fullscreen mode
  • Command Line for Repository Management

Efficient repository management involves regularly checking the status, adding, committing, and handling changes made to your code.

  • Checking repository status

Use the "git status" command to see the current state of your repository.

kannan@kannan-PC:~$ cd samplerepo
kannan@kannan-PC:~/samplerepo$ ls
README.md
kannan@kannan-PC:~/samplerepo$ touch testfile.txt
kannan@kannan-PC:~/samplerepo$ git status
On branch main
Your branch is up to date with 'origin/main'.

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

nothing added to commit but untracked files present (use "git add" to track)
Enter fullscreen mode Exit fullscreen mode

It shows which files are modified, staged, or untracked.

  • Adding and committing changes

Add files to the staging area with the "git add" command.

kannan@kannan-PC:~/samplerepo$ git add testfile.txt
kannan@kannan-PC:~/samplerepo$ git status
On branch main
Your branch is up to date with 'origin/main'.

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

Enter fullscreen mode Exit fullscreen mode

Commit and push your changes using the "git commit"
"git push"command, providing a descriptive message.

kannan@kannan-PC:~/samplerepo$ git commit -m "commit testfile"
[main a588666] commit testfile
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 testfile.txt

kannan@kannan-PC:~/samplerepo$ git push origin main
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 8 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 440 bytes | 440.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0), pack-reused 0
To github.com:kannanb95/samplerepo.git
   322930c..c3f05aa  main -> main


Enter fullscreen mode Exit fullscreen mode
  • Reverting commits

Undo commits with the "git revert" command to create a new commit that reverses the changes.

kannan@kannan-PC:~/samplerepo$ git reflog
a588666 (HEAD -> main) HEAD@{0}: checkout: moving from main to main
a588666 (HEAD -> main) HEAD@{1}: commit: commit testfile
13fa75f (origin/main, origin/HEAD) HEAD@{2}: clone: from github.com:kannanb95/samplerepo.git
kannan@kannan-PC:~/samplerepo$ git revert a588666
[main 18c1c71] Revert "commit testfile"
 1 file changed, 0 insertions(+), 0 deletions(-)
 delete mode 100644 testfile.txt

Enter fullscreen mode Exit fullscreen mode
  • GitHub Collaboration with CLI

Collaboration on GitHub is enhanced through the use of branches, which allow multiple developers to work on different features simultaneously.

Image description

They enable parallel work on different features or bug fixes.

  • Creating and switching branches

Create a new branch with the "git branch" command.

kannan@kannan-PC:~/samplerepo$ git branch project
kannan@kannan-PC:~/samplerepo$ git branch testing
kannan@kannan-PC:~/samplerepo$ git branch
* main
  project
  testing

Enter fullscreen mode Exit fullscreen mode

Switch between branches using the "git checkout" command.

kannan@kannan-PC:~/samplerepo$ git checkout project
Switched to branch 'project'
kannan@kannan-PC:~/samplerepo$ git branch
  main
* project
  testing
Enter fullscreen mode Exit fullscreen mode
  • Merging branches

Merge branches together using the "git merge" command.

This incorporates changes from one branch into another.

kannan@kannan-PC:~/samplerepo$ git branch
  main
* project
  testing
kannan@kannan-PC:~/samplerepo$ ls
README.md  testfile2.txt  testfile3.txt  testfile.txt
kannan@kannan-PC:~/samplerepo$ git checkout main
Switched to branch 'main'
kannan@kannan-PC:~/samplerepo$ git branch
* main
  project
  testing
kannan@kannan-PC:~/samplerepo$ ls
README.md  testfile2.txt  testfile3.txt
kannan@kannan-PC:~/samplerepo$ git merge project 
Updating 18c1c71..1e5971a
Fast-forward
 testfile.txt | 0
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 testfile.txt
kannan@kannan-PC:~/samplerepo$ ls
README.md  testfile2.txt  testfile3.txt  testfile.txt
Enter fullscreen mode Exit fullscreen mode
  • Adding and managing remotes

Add a remote repository with the "git remote add" command.

git remote add origin git@github.com:User/UserRepo.git
Enter fullscreen mode Exit fullscreen mode

Pushing and pulling changes

Push local changes to a remote repository using the "git push" command.

kannan@kannan-PC:~/samplerepo$ git push origin project
Enumerating objects: 6, done.
Counting objects: 100% (6/6), done.
Delta compression using up to 8 threads
Compressing objects: 100% (4/4), done.
Writing objects: 100% (5/5), 580 bytes | 580.00 KiB/s, done.
Total 5 (delta 1), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (1/1), done.
remote: 
remote: Create a pull request for 'project' on GitHub by visiting:
remote:      https://github.com/kannanb95/samplerepo/pull/new/project
remote: 
To github.com:kannanb95/samplerepo.git
 * [new branch]      project -> project

Enter fullscreen mode Exit fullscreen mode

Fetch and merge remote changes into your local repository with the "git pull" command.

kannan@kannan-PC:~/samplerepo$ git pull origin project 
From github.com:kannanb95/samplerepo
 * branch            project    -> FETCH_HEAD
Already up to date.
Enter fullscreen mode Exit fullscreen mode
  • Reviewing changes using CLI tools

Use the "git diff" command to view changes between commits, branches, or files.

kannan@kannan-PC:~/samplerepo$ git status
On branch project
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
    new file:   testfile2.txt
    new file:   testfile3.txt

kannan@kannan-PC:~/samplerepo$ git diff
kannan@kannan-PC:~/samplerepo$ cat > testfile2.txt
hi i have made some changes here
kannan@kannan-PC:~/samplerepo$ git diff
diff --git a/testfile2.txt b/testfile2.txt
index e69de29..7859618 100644
--- a/testfile2.txt
+++ b/testfile2.txt
@@ -0,0 +1 @@
+hi i have made some changes here

Enter fullscreen mode Exit fullscreen mode

Understand which lines were added, modified, or removed.

  • Few Git Cmds Rebase between branch and main
git rebase 
Enter fullscreen mode Exit fullscreen mode

To save the work files on branch (before add&commit)

kannan@kannan-PC:~/samplerepo$ git stash
Saved working directory and index state WIP on project: 1e5971a commit file
Enter fullscreen mode Exit fullscreen mode

To find git log status

kannan@kannan-PC:~/samplerepo$ git log --oneline
1e5971a (HEAD -> project, origin/project) commit file
18c1c71 (testing, main) Revert "commit testfile"
a588666 commit testfile
13fa75f Initial commit

Enter fullscreen mode Exit fullscreen mode

Git File move cmds "git mv"

git mv -f file1.txt file2.txt
git mv -nf file1.txt file2.txt
git mv -k source.txt destination.txt 
git mv -v filea.txt fileb.txt
Enter fullscreen mode Exit fullscreen mode

Git reset cmds "git reset"

git reset --hard <commit id>    complete delete files
git reset -- soft  <commit id>    under staged state
git reset --mixed <commit id>   its default and unstaged state
Enter fullscreen mode Exit fullscreen mode

To view commit status "git show "

kannan@kannan-PC:~/samplerepo$ git show a588666
commit a588666b9499a54da06eafca87e44ebb193c8c4c
Author: kannanb95 <142100996+kannanb95@users.noreply.github.com>
Date:   Sat Sep 2 23:14:52 2023 +0530

    commit testfile

diff --git a/testfile.txt b/testfile.txt
new file mode 100644
index 0000000..e69de29

Enter fullscreen mode Exit fullscreen mode

Git show cmds

git show --pretty=oneline <commit_id>
git show --pretty=short <commit_id>
git show --pretty=medium <commit_id>
git show --pretty=full <commit_id>
git show --pretty=fuller <commit_id>
git show --pretty=raw <commit_id>
Enter fullscreen mode Exit fullscreen mode

Using SSH for GitHub Passwordless Authentication
Authenticating with GitHub typically involves using an access token or password. However, these methods can be inconvenient and insecure, especially when accessing GitHub from multiple devices.

GitHub provides the option to use Secure Shell (SSH) for authentication. SSH presents a secure network protocol for remote machine access. This feature proves especially valuable in situations that demand automation or frequent remote access.

  1. Setting Up SSH for GitHub
ssh-keygen -t rsa -b 4096 -C "youremail@domain.com"

Generating public/private rsa key pair.
Enter file in which to save the key (/home/vagrant/.ssh/id_rsa):
Enter fullscreen mode Exit fullscreen mode

Run the following cmd to get the public key content

cat ~/.ssh/id_rsa.pub
Enter fullscreen mode Exit fullscreen mode
  1. Adding the Public Key to GitHub
    Log in to your GitHub account and go to your Account Settings.
    Click on SSH and GPG keys located on the left sidebar.
    Click on New SSH key.
    Give your SSH key a Title.
    Paste the public key content into the Key field.
    click on the Add SSH key to save the SSH key to your GitHub account.

  2. Configuring an SSH Agent

eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_rsa
Enter fullscreen mode Exit fullscreen mode

Now you can explore the passwordless Authentication using SSH.
We can also explore with different methods like "cached method","unset method" and "stored method".

Summary

In this comprehensive guide, we covered a wide range of command line commands for mastering GitHub. By familiarising yourself with these essential tools, you've taken a significant step towards unleashing the full potential of GitHub in your development journey. Remember, continuous learning and practice will further solidify your understanding and expertise. Enjoy your supercharged GitHub adventures!

Top comments (0)