---
# System prepended metadata

title: git basic
tags: [git, revision control]

---

# git basic
######  tags: `git` `revision control`

--------------------------------

# Introduction

## fetch vs pull


--------------------------------
# Git in MS-Windows

## `git clone` an Existing Repository in MS-Windows
Normally `cd <repo_dir>; git clone <https_URL>/<project.git>` command is used in Linux to clone an existing online git archive to the local repository, however, CLI is not all that accessible in MS-Windows.  Normally git-GUI or VSCode client is used to retrieve the git repository.

### `git clone` with ssh server
*NOTE*: the ssh server must have git installed as `git clone` just uses ssh for transfer the git data.
https://help.github.com/en/articles/which-remote-url-should-i-use
SSH URLs provide access to a Git repository via SSH, a secure protocol. To use these URLs, you must generate an SSH keypair on your computer and add the public key to your GitHub account. For information on setting up an SSH keypair, see "Generating an SSH key."

When you git clone, git fetch, git pull, or git push to a remote repository using SSH URLs, you'll be prompted for a password and must provide your SSH key passphrase.

If you are accessing an organization that uses SAML single sign-on, you won't be able to clone with SSH. Instead, clone with the HTTPS URL.

Tip: SSH URLs can be used locally, or as a secure way of deploying your code to production servers. You can also use SSH agent forwarding with your deploy script to avoid managing keys on the server.



If you use the alternate form of ssh URLs you don't need an absolute path. For example...

git clone lars@myserver.example.com:repos/myrepo.git
...will clone repository repos/myrepo.git relative to my home directory, although this doesn't permit use of an alternate port. However, you can also use ~ in either form to indicate the user's home directory, e.g.:

git clone ssh://login@server.com:12345/~/repository.git

### git-GUI/VSCode repo cloning from https_URL


### git-GUI/VSCode repo cloning from MS-Windows CIFS Shared Directory
This does make the git cloning operation a bit strange.  For example, the git repository sits on `\\nleids6500-transit-nas01.sn-eu.asml.com\HOME_SDEV$\lchu\leoscript\` CIFS shared directory and the backward slash must be translated into the forward slash, so that the cloning command becomes: 
`git clone //nleids6500-transit-nas01.sn-eu.asml.com/HOME_SDEV$/lchu/leoscript c:\Users\lchu\repo\leoscript`

[Some people](https://stackoverflow.com/questions/2519933/git-clone-repo-across-local-file-system-in-windows/2520121) were saying `file://` notation shall be used instead.

You can specify the remote’s URL by applying the UNC path to the file protocol. This requires you to use four slashes:

`git clone file:////<host>/<share>/<path>`
For example, if your main machine has the IP 192.168.10.51 and the computer name main, and it has a share named code which itself is a git repository, then both of the following commands should work equally:
```
git clone file:////main/code
git clone file:////192.168.10.51/code
```

Leo: below is what works for me.
`git clone --no-hardlinks /path/to/repo`
The above command uses POSIX path notation for the directory with your git repository. For Windows it is (directory C:/path/to/repo contains .git directory):

`C:\some\dir\> git clone --local file:///C:/path/to/repo my_project`
The repository will be clone to C:\some\dir\my_project. If you omit `file:///` part then `--local` option is implied.

## Modify Files in git Repository
git shall automatically track the changes you've made to the files that are currently in git repository.  Unless it's a new file, then `git commit -a -m "your-comment"` shall just commit your changes to your local git repository.

`git add -i` - add files to the local git repository interactively.

## Identify the Remote Repository Linked (OPTIONAL)
A remote origin is automatically added when a local repository is `git clone` from a remote location.  Anyway, multiple remote repositories can be defined in your local git repository.
`git remote -v` - show all remote repositories that are currently linked to.  Normally just one remote repository where the local repository is cloned from.
`git remote add <remote_repo_name> <https_URL>` - add another remote repository to the existing git repo (for redundancy???)

In VSCode, just launch a terminal open your git repository is opened.  The terminal will automatically change the directory to your repository, then issue `git remote -v` command to identify the repo's remote(s).
```
C:\Users\lchu\repo\leoscript>git remote -v
origin  //nleids6500-transit-nas01.sn-eu.asml.com/HOME_SDEV$/lchu/leoscript (fetch)
origin  //nleids6500-transit-nas01.sn-eu.asml.com/HOME_SDEV$/lchu/leoscript (push)
```

## Push Changes in The Local Repository to The Remote
`git push <remote_repo_name> <branch>` - Push latest changes in your local repository to its remote, eg: `git push ORIGIN master`.
`git push origin master:master`


```
git checkout master
```
(If you're on a different branch than master, use the branch name there instead.)

If that doesn't work, try...

For a single file:
```
git checkout HEAD /path/to/file
```
For the entire repository working copy:
```
git reset --hard HEAD
```

### Push Problem Encountered
Leo: When I tried to push changes stored on my laptop onto its origin (remote network files on my $HOME drive which has a slower access), the following error came up.  I couldn't understand its problem as I have seen this type of problem in SCCS/CVS/Perforce before.  At the end, I figured out it's due to the REMOTE repository is on the same branch as my LOCAL repository.  To workaround this issue, just switch to a different branch on the remote repository.

This is annoying.  Maybe the best practice would be working on "lchu_dev" branch and constantly update my code to this branch (whenever there is a new incremental feature added OR at the end of day), then merge the changes to the HEAD when all features are completed and tested.  This is philosophy of git: version control every incremental changes rather than one big BULK. 

```
C:\Users\lchu\repo\leoscript>git push
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 8 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 346 bytes | 346.00 KiB/s, done.
Total 3 (delta 2), reused 0 (delta 0)
remote: Checking connectivity: 3, done.
remote: error: refusing to update checked out branch: refs/heads/master
remote: error: By default, updating the current branch in a non-bare repository
remote: is denied, because it will make the index and work tree inconsistent
remote: with what you pushed, and will require 'git reset --hard' to match
remote: the work tree to HEAD.
remote:
remote: You can set the 'receive.denyCurrentBranch' configuration variable
remote: to 'ignore' or 'warn' in the remote repository to allow pushing into
remote: its current branch; however, this is not recommended unless you
remote: arranged to update its work tree to match what you pushed in some
remote: other way.
remote:
remote: To squelch this message and still keep the default behaviour, set
remote: 'receive.denyCurrentBranch' configuration variable to 'refuse'.
To //nleids6500-transit-nas01.sn-eu.asml.com/HOME_SDEV$/lchu/leoscript
 ! [remote rejected] master -> master (branch is currently checked out)
error: failed to push some refs to '//nleids6500-transit-nas01.sn-eu.asml.com/HOME_SDEV$/lchu/leoscript'
```


## Pull vs Fetch
When I tried to push the changes in my local repository to its remote, VSCode asked me about "Shall fetch be periodically performed at the background for you".  This is interesting.  Pull/Fetch will ensure my local repository is up-to-date before I push the changes.  But where are the differences between Pull and Fetch?


-----------------------------------
Reference
[1] https://git-scm.com/book/en/v2/Getting-Started-About-Version-Control





<h1> Git Tagging </h1>
https://git-scm.com/book/en/v2/Git-Basics-Tagging 

