DEV Community

ShulyAvraham
ShulyAvraham

Posted on • Edited on

OSDC Lesson 3: GitHub CLI

Git CLI

Install git CLI

Work on my repo via CLI

To work locally on the repository owned by me via the command line I first need to clone it.

Clone the repository locally

  • Go to my repo online
  • Click Code
    • Select either HTTPS or SSH
      • SSH: Recommended!
        • We will need authentication in the form of SSH key
      • HTTPS: In case for some reason the SSH will not work, we can use HTTPS since it's a public project, but it will require authentication for every pull and push.
    • Open the installed command line (e.g. bash on Linux, GitHub CLI)
    • Copy the URL to paste in the git clone command
    • Answer yes to the question whether you're sure you want to connect...
git clone git@github.com:ShulyAvraham/shulyavraham.github.io.git
Enter fullscreen mode Exit fullscreen mode
  • A folder with the repository is created locally.
  • This folder can be moved anywhere, as all the information is contained within it.
  • I can now edit files in any editor or via the command line
  • To run git commands I should enter this folder
ls -l
cd shulyavraham.github.io.git
Enter fullscreen mode Exit fullscreen mode

Git CLI commands

Communicate between my local repository and my own remote repository (vs. a public open source repo not owned by me).

The recommendation for continuous integration is for all users to work on the same main branch. This is to quickly identify and prevent conflicts during merge.

Edit files and interact with git

Open a file using some editor, edit then save it.

From the command line:

Let's see what changed in git

git status
Enter fullscreen mode Exit fullscreen mode

It will show that the file was modified (since the last commit). Now the file is only changed locally, but not in git. In order to add it to git:

git add filename
Enter fullscreen mode Exit fullscreen mode

Another git status will reveal that the file was added to git's staging area. In order add it to the git repo we will commit the changes:

git commit -m'a comment describing the change'
Enter fullscreen mode Exit fullscreen mode

The first time I communicate with git it will recommend me to configure my username and email. These are not being checked, but used to identify whom preformed which commit.

git config --global user.name "Shuly Avraham"
git config --global user.email"Shuly.Avraham@mymail.com"
Enter fullscreen mode Exit fullscreen mode

After the commit, another git status will reveal that there's nothing to commit and that my tree is clean.

However, my branch is ahead of origin/main. origin represents the remote repository. So that my local branch is 1 commit ahead of the remote branch.

To push my change to my remote repo

git push
Enter fullscreen mode Exit fullscreen mode

Or

git push origin main
Enter fullscreen mode Exit fullscreen mode

Another git status will reveal that my local repo is identical to the remote repo, of the main branch.

Since I did git push, I can now see the changes on the remote site.

Add a new file. git status will show that the file is untracked.

Modify another file.

Now we have two files that are unstaged. We can add all the modified to the staging area

git add .
Enter fullscreen mode Exit fullscreen mode

Now the next commit will include both changes.

If I want only specific file or files to be included in a specific commit, even though I made changes to other files as well, I will specify the file names in the git add, and then commit.

git add somefilename
git commit -m'A specific change'
Enter fullscreen mode Exit fullscreen mode

It is advised to include only files with related changes in a specific commit.

To check what changed since the last commit to my local changes

git diff
Enter fullscreen mode Exit fullscreen mode

To see changes since last commit to my staging area

git diff --staged
Enter fullscreen mode Exit fullscreen mode

To restore files from staging area

git restore --staged filename
Enter fullscreen mode Exit fullscreen mode

To see my history of commits

git log
Enter fullscreen mode Exit fullscreen mode

A tool to view changes

gitk --all
Enter fullscreen mode Exit fullscreen mode

Cancel and delete last commit. NOT RECOMMENDED

git reset HEAD~1
Enter fullscreen mode Exit fullscreen mode

The number represents the number of commits back.

The change is still in my local file. To delete also the local change

git reset HEAD~1 –hard

Enter fullscreen mode Exit fullscreen mode

To see a change in a specific commit

git show sha_of_commit
Enter fullscreen mode Exit fullscreen mode

To see the difference between a commit and what I have now

git diff sha_of_commit
Enter fullscreen mode Exit fullscreen mode

Some more commands

git rm --cached <file>
Enter fullscreen mode Exit fullscreen mode

Remove from staging

git reset file
Enter fullscreen mode Exit fullscreen mode

Remote mapping

git remote -v
Enter fullscreen mode Exit fullscreen mode

We will see the mapping of the remote repos for fetch (pull) and push. Normally origin, but this can be changed. Also, we can have different remotes for pull and push.

The .git directory

Inside the root dir of our local repo

ls -l
Enter fullscreen mode Exit fullscreen mode

Will reveal the .git directory which contains all the changes to the git repo.

config file contains git configuration. (such as user info, remote repo info, etc.]

It can be edited directly or by git configuration commands via the command line which will keep changes in this file.

Top comments (0)