Here’s a precise manual I put together for convenient learning of Git and GitHub management and as an easy reference. Bookmark or re-download periodically as I will keep updating this page for improvement. Installation and some other instructions are currently only for Mac OS X operating system.


\[\\[0.4in]\]

Configure

Initialize a local git repository (creates .git folder in the current directory)

git init

Configure git (set/unset user names and emails at system, global and local levels)

git config --system user.name "Macbook pro"
git config --global user.name "nilotpalsanyal"
git config --local user.name "BHMSMAfMRI"

=>NOTE: Using system option configures for *all users* in the system 
        (laptop) [[path]/etc/gitconfig file]. As this is a system 
        configuration file, you would need administrative/superuser 
        privilege to make changes to it. Using global option configures for 
        a *specific user* in the system. [~/.gitconfig or ~/.config/git/
        config file]. This affects all of the repositories you work with on 
        your system.) Using local option configures for a *specific user* 
        in the system.[.git/config file of any repository] (specific to 
        that single repository. You can force Git to read from and write to
        this file with the -- local option, but that is in fact the 
        default. Unsurprisingly, you need to be located somewhere in a Git 
        repository for this option to work properly.) Each level overrides 
        values in the previous level.
      

See configured user names and emails (at system, global and local levels)

git config --system --list
git config --global --list
git config --local --list

Unset some configuration (or simply edit the config text file)

git config --global --unset user.name

\[\\[1in]\]

Set-up SSH

Create/generate SSH key

ssh-keygen -t ed25519 -C "comment"

=>NOTE: After running the above, keep on pressing Enter to avoid setting
        passphrase till the key's randomart image appears. There are 4 
        generation methods for a random ssh key dsa--ecdsa, ed25519, rsa, 
        the last one being default. Github recommends ed25519) ssh-keygen 
        by default writes keys in an OpenSSH-specific format. This format 
        is preferred as it offers better protection for keys at rest as 
        well as allowing storage of key comments (-C) within the private 
        key file itself. The key comment may be useful to help identify 
        the key. The comment is initialized to "user@host" when the key is 
        created, but can be changed using the -c option. 

Add SSH private key to the SSH-agent

eval "$(ssh-agent -s)" # start the ssh-agent in the background
ssh-add ~/.ssh/id_ed25519

Add SSH public key to the github account (github -> Settings -> SSH and GSA keys -> add new SSH key)

pbcopy < id_ed25519.pub

Generate a personal access token

If you are pushing (see below) to a public repository on GitHub, you do not need a personal access token every time you push. However, if you are pushing to a private repository, you will need to authenticate yourself using a personal access token. You can generate a personal access token for your GitHub account by following these steps:

  • Log in to your GitHub account.
  • Click on your profile icon in the top-right corner of the page.
  • Select “Settings” from the dropdown menu.
  • In the left-hand menu, click on “Developer settings.”
  • Select “Personal access tokens” from the options on the left.
  • Click the “Generate new token” button.
  • Give your token a descriptive name and select the scopes you want the token to have access to.
  • Click the “Generate token” button.
  • Copy the token to your clipboard and keep it in a secure location.
  • Note that your access token is like a password, so be sure to keep it secure and do not share it with anyone. You can use your access token to authenticate with the GitHub API or to access your account through the command line.

\[\\[1in]\]

Repository / Branch

Connect a remote repository (URL) and give it an alias/nickname ‘origin’

git remote add origin https://github.com/nilotpalsanyal/GWASinlps.git #Using HTTPS

git remote add origin git@github.com:nilotpalsanyal/GWASinlps.git #Using SSH

=>NOTE: In git, "origin" is an alias/shorthand name for the remote    
        repository that a project was originally cloned from. No need to  
        name the remote repository origin. In fact, the same repository 
        could have a different alias for another developer.

Check alias of the remote repo(s) in your local system

git remote

Check what URL belongs to each remote and more information

git remote -v
git remote show origin

Rename the remote alias ‘origin’ to ‘newalias’

git remote rename origin newalias

Change the URL in the origin

git remote set-url origin newurl_here

Fetch commits from a remote repo/branch
(for a connected repo locally aliased origin)

git fetch origin #fetches info about all branches of the remote

git fetch origin branchname #fetches info about the given branch of the remote

=>NOTE: It gathers any commits from the target branch that do not 
        exist in the current branch and stores them in your local 
        repository. However, it does not merge them with your 
        current branch.

Merge fetched commits from a remote branch with local branch
(for a connected repo locally aliased origin)

git merge origin branchname

Reset merge in case of conflict

git reset –merge

Pull files from a remote branch to local branch (for a connected repo locally aliased origin)

git pull origin branchname

=>NOTE: `pull` is `fetch` + `merge`.

Push local branch files to a remote branch (of a connected repo)

git push -u origin main

See current branch

git branch --show-current

=>NOTE: When inside a directory you initialize git with `git init`, the
        default branch that gets created is named master. Check it by 
        this command. The reference of this default branch (or all other
        branches) is written inside the file .git/HEAD. Note that, 
        you cannot delete the default branch or the last existing branch 
        by any git command, can only rename it.

See all local and remote branches

git branch / ‘-a’ / -all / --list #all branches
git branch -r #remote branches
git branch -l #local branches

=>NOTE: You need to make at least one commit before you can see 
        branches listed by the `git branch` command. If no commit
        yet, can be seen only using --show-current option.

Create a new local branch with current branch files

git branch branchname

Create a new local branch with current branch files and move to it

git checkout -b branchname

Create a new local branch from a remote branch of repo origin

git branch newbranch origin/remotebranch

Delete a local branch

git branch -d branchname

=>NOTE: You cannot delete a branch while using it. Have to move to another
        branch first.

Delete a remote branch

git push origin --delete branchname

Rename a branch

git branch -m AA #renames the current branch as AA
git branch -m BB CC #renames branch BB as CC

Use/Move to a branch

git checkout branchname / git switch branchname

Remove a branch reference

rm .git/refs/heads/branchname

Create a file

touch filename

Check if any file in current folder (local branch) is modified

git status

If a file is modified but not yet staged, can remove the modification

git checkout filename #a second use of checkout option

Place the modified file(s) in a staging area

git add filename
git add -f filename #if filename is listed in ignore-list, you can force add using -f
git add .

Commit the change

git commit -m "update"

=>NOTE: Commit means to take everything from the staging area and make a
        permanent snapshot of the current state of your repository that is
        associated with a unique identifier.

Edit the message of the last commit

git commit –amend -m “New commit message”

Undo last commit

git reset HEAD~

Push (upload) to an existing branch

git push origin branchname

Push (upload) to a new branch

git push origin NS:username/reponame branchname #replace NS by your local host id

=>NOTE: Clone: Copy online repository to local computer 
        Commit: Modify files in your repository and track changes
        Push: copy changes in local computer to online repository

See existing references

git show-ref

=>NOTE: A ref is an indirect way of referring to a commit. You can think of
        it as a8-friendly alias for a commit hash. Due to the recent 
        "Replacing master with main in GitHub" action, you may notice that 
        there is a refs/heads/main.

See files/ folders in .gitignore list (these are not tracked or pushed)

git check-ignore *
git check-ignore -v filename #identify which pattern ignores filename

Add/ Remove files to/ from ignore-list

Simply edit the .gitignore file

After adding a file to ignore-list, remove it (previous uploads) from the remote

git rm -r --cached filename #this removes only the caches of this file from git, does not delete the file
git add . #add everything afresh. This file won’t be added now as it is in ignore-list
git commit -m "removed unwanted files"
git push origin branchname

Pull a file from branch sub to current branch

git checkout sub filename

Create file/folder in one local branch, then transfer to another local branch

#create file/folder NEW #in branch main
git add NEW #track it
git stash #store the changes in a separate space (NEW will vanish from main here)
git switch branch2 #move to another branch
git rm -r --cached NEWS #remove previous caches of NEWS, if necessary
git stash pop #pick up the changes (NEW will drop here)
git add . ; git commit -m "bla" ; git push #push the changes
git switch main #come back to main branch. NEW will not be there.

Make changes, store separately, apply chosen ones

#make a change #to a file
git stash #store change separately (file changed back after this)
#make another change
git stash #store change separately (file changed back after this)
git stash list #see list of stashes
git stash apply #apply to file changes from all stashes
git stash apply 2 #apply to file changes from stash 2 only
git stash clear #clear all stashes

Discard all (uncommitted) changes (be careful!)

git reset --hard

Delete untracked files (be careful!)

git clean -fd

Delete all files in a remote repo

git pull origin #to make sure local repo is up to date
git rm -r .
git add -all
git commit -m “Remove all files from the repository”
git push origin