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$
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
- Creating and deleting files and directories
Create a new file using the "touch" "vim" command.
kannan@kannan-PC:~/newrepo$ touch testfile.txt
Remove files or directories with the "rm" command.
kannan@kannan-PC:~/newrepo$ rm testfile.txt
- 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
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
- 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.
- 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.
- 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)
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
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
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
- GitHub Collaboration with CLI
Collaboration on GitHub is enhanced through the use of branches, which allow multiple developers to work on different features simultaneously.
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
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
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
- Adding and managing remotes
Add a remote repository with the "git remote add" command.
git remote add origin git@github.com:User/UserRepo.git
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
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.
- 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
Understand which lines were added, modified, or removed.
- Few Git Cmds
Rebase between branch and main
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
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
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
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
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
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>
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.
- 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):
Run the following cmd to get the public key content
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.
Configuring an SSH Agent
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_rsa
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!