OSDC Lesson 3: GitHub CLI
Tags: osdc, opensource, github, programming
Published: 2023-01-28 16:42:48

---
series: OSDC
---
# Git CLI
[Install git CLI](https://dev.to/shulyavraham/install-git-cli-2lag)

## 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](https://github.com/ShulyAvraham/shulyavraham.github.io/) online 
* Click `Code` 
  * Select either `HTTPS` or `SSH`
      * `SSH`: Recommended! 
          * We will need authentication in the form of [SSH key](https://dev.to/shulyavraham/ssh-key-pair-3ij8)
      * `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
```
* 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
```

## 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
```
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
```
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'
```
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"
```
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
```
Or 
```
git push origin main
```
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 .
``` 
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'
```
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
```
To see changes since last commit to my staging area
```
git diff --staged
```
 To restore files from staging area
```
git restore --staged filename
```
To see my history of commits
```
git log
```
 A tool to view changes
```
gitk --all
```
Cancel and delete last commit. **NOT RECOMMENDED**
```
git reset HEAD~1
```
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

```
To see a change in a specific commit
```
git show sha_of_commit
```
To see the difference between a commit and what I have now
```
git diff sha_of_commit
```
Some more commands
```
git rm --cached <file>
```
Remove from staging
```
git reset file
```

### Remote mapping
```
git remote -v
```
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
```
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.