Skip to content

jonyktan/selfhelp-git

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 

Repository files navigation

git-selfhelp

Personal Git cheat sheet

Contents

Setting Up

Setting up git credentials on new machine.

Click to expand

Reference: GitHub Docs for SSH keys

  1. Check for existing SSH key on machine

  2. Generate new SSH key if none available

  3. Add SSH key to GitHub account

  4. Use SSH URL for git clone (e.g. ssh://[email protected]/username/repo.git). Edit in .git/config if previously cloned using https or git URL. See reference here.

Cloning and Changing Remotes

Cloning Only

Click to expand

Objective

To quickly clone a public repo into a private repo. Caveat: clone of public repo on local machine is not usable for development.

Steps

  1. Clone the public repo onto local machine. This includes all branches and commit history, but without any link back to the Github repo, i.e. you will not able to git fetch again.

    git clone --bare <public repo url> <local folder name>
    
  2. Create private repo on Github.

  3. Enter local folder.

    cd <local folder name>
    
  4. Push the local version of the contents of public repo to the private repo.

    git push --mirror <private repo url>
    
  5. Delete local folder.

    cd ..
    
    rm -rf <local folder name>
    
  6. Clone private repo.

    git clone <private repo url> <local folder name>
    
  7. To setup the new local folder to track changes in the public repo, see steps 7 onward in the next section to create a tracking remote.

Clone and Track Updates

Click to expand

Objective

To clone a public repo into a private repo and track changes of the public repo.

Steps

  1. Clone the public repo onto local machine.

    git clone <public repo url> <local folder name>
    
  2. Create private repo on Github.

  3. Enter local folder.

    cd <local folder name>
    
  4. Change remote for origin for local folder to private repo. origin is the default pull/push Git repo for local folder.

    git remote set-url origin <private repo url>
    
  5. Check that the origin remote has been changed.

    git remote -v
    
     > origin <private repo url> (fetch)
     > origin <private repo url> (push)
    

    Alternatively, check that the remote "origin" is set to private repo url in the git config file.

    cat .git/config
    
  6. Push cloned files to main branch on private repo. This also sets the upstream for main to the origin remote.

    git push origin main
    
  7. Create branch to track public repo.

    git branch <tracking branch name>
    
  8. Create new remote to track public repo.

    git remote add <new remote name> <public repo url>
    
  9. Check that the new tracking remote has been created.

    git remote -v
    
  10. Fetch the branch info from the new tracking remote (aka the public repo).

    git fetch <new remote name>
    
  11. Change the upstream for the new tracking branch to the new tracking remote.

    git branch <tracking branch name> --set-upstream-to <tracking remote name>/<branch on public repo>
    
  12. Check that the main development branch is tracking the private repo and the tracking branch is tracking the public repo.

    cat .git/config
    

Co-Development

Click to expand

Managing Branches

Create new feature on new working branch.

  1. Create new working branch from existing branch (e.g. 'development').

    git checkout -b <new branch name> <existing branch name>
    
  2. Push the new branch to Github (or remote).

    git push --set-upstream <remote name> <new branch name>
    

Managing Changes

Click to expand

Squashing Commits in General

  1. View commit history.

    git log --oneline
    
  2. Squash x past commits.

    git rebase -i HEAD~<x>
    
    1. An editor will open.
    2. Use pick or p to retain specific commits. Use squash or s to combine specific commits.
    3. Once done, save the file and close.
    4. Another editor will open with the new commit messages for further editing if required.
    5. Once done, save the file and close.

Squashing Commits During Merge

To clean up commits on one branch (e.g. dev branch) when merging it into another (e.g. main branch).

  1. Switch to main branch.

    git switch <main branch>
    
  2. Use --squash flag when merging incoming branch (e.g. dev branch).

    git merge --squash <dev branch>
    
  3. Commit the changed files with a single commit message.

  4. Push to recipient branch (e.g. main branch), create Pull Request on GitHub, and merge Pull Request.

Undoing Commits

  1. Check past commits.

    git reflog
    
  2. Reset current branch to desired commit (HEAD@{x}).

    git reset --hard HEAD@{x}
    

    For Windows:

    git reset --hard "HEAD@{x}"
    

Handling Merge Conflicts

Click to expand

Unable to Git Pull as Local Repo Commits have Diverged from Remote

  • Fetch changes made to the upstream since the last git fetch, merge to local branch, and apply existing local commits to the top of the updated local branch (aka "rebasing the local branch on top of the upstream branch after fetching"). (Explainer)

    git pull --rebase
    

Handling Line Endings

Click to expand

Set global default line endings for Linux

git config --global core.autocrlf input

Set global default line endings for Windows

git config --global core.autocrlf true

Submodules

Click to expand

Add submodule to Repo

  1. Clone submodule into repo.

    git submodule add <submodule URL>
    
  2. Commit and push changes to origin (new submodule folder and new .gitmodules file).

Clone Repo with Submodules

  1. Clone the repo.

  2. Initialise submodule.

    git submodule init
    
  3. Fetch data from submodule project.

    git submodule update
    

Cleaning Up

Click to expand

Delete Branch

git branch -d <old branch>
  • Use -D to force delete.

About

Personal Git cheat sheet

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published