# Git

# Git global setup

  1. git config --global user.name "..."
  2. git config --global user.email [email protected]
  3. git config --global alias.co checkout
  4. git config --global core.editor "mvim -f"

# Basics

# git add

# git commit

  • git add . => git commit -a

# git diff

# git stash

# .gitignore

# Undoing Changes

  • git clean

  • git revert

  • git reset

  • git reset HEAD~2 Roll back to last 2 commit

  • git reset --hard HEAD Return to last commit state, delete previous changes**

  • git init Intialize local git

  • git add . Git add all files

  • git status

  • git log

  • git branch List all branches

  • git branch <new branch> Create new branch with name

  • git checkout fileName.txt Checkout previous commit of fileName.txt

  • git checkout -f Checkout previous commit and overriding current changes

  • git checkout -b ... Create and switch new branch with name ...

  • git commit -a -m "..." Commit all changes with messages ...

  • git checkout master Checkout to master

  • git merge ... Merge branch to master

  • git branch -d branchName Delete branchName -d=delete commited changes only, -D=delete uncommitted changes

  • git push origin --delete remoteBranchName Delete branch remotely

  • git fetch -p Synchronize branch list, -p "prune"

# Change only one file

  1. git checkout -- hello.rb
  2. git checkout HEAD hello.rb
  3. git clean -d -x -n Clean up uncommited files -d including directories, -x files ignored by git, -f dry run

Send changes to remote server

git push REMOTE NAME-OF-BRANCH

Exisiting folder

cd existing_folder git init git remote add origin https://gitlab.com/jycm/simple.git git add . git commit -m "Initial commit"

  • git remote add origin https://github.com/address.git Add remote git repository to local repo.

  • git push -u origin master Push local to remote repository using the -u flag/option (link up local/remote), "origin" name of remote repository and "master" name of branch.

  • git remote show origin Show remote git repo details

  • :q to get out of git log

  • Add files to .gitignore

# Push Branch to Remote

git push <remote> <branch>

// example push "feature" branch to "origin remote"
git push origin feature

//If upstream branch not created, need to run "-u" command with "git push".
git push -u origin feature

Push Branch to another Branch

git push <remote> <local_branch>:<remote_name>

//example
git push origin feature:develop

Before pushing branch, may need to merge remote branch to current local branch. Pull changes from remote branch to current local branch

$ git pull
$ git checkout feature
$ git merge origin/develop
$ git push origin feature:develop

Push branch to another Repository

$ git push <remote> <branch>
//See remotes in repo
$ git remote -v
// psuh branch to "custom" remote 
$ git push custom feature

# SSH

# Check for exisiting SSH keys

$ ls -al ~/.ssh
# Lists files in exisiting .ssh directory, the default files one of following:
id_rsa.pub
id_ecdsa.pub
id_ed25519.pub

# Generate new SSH key

$ ssh-keygen -t rsa -b 4096 -C "[email protected]"

# Testing ssh connection

ssh -T [email protected] where [email protected] is the instance domain.

# References

Last Updated: 8/1/2021, 4:23:23 AM