Skip to content

Instantly share code, notes, and snippets.

@rendorHaevyn
Last active February 11, 2021 06:05
Show Gist options
  • Save rendorHaevyn/914d3156cd72c135df7308fac3a5b104 to your computer and use it in GitHub Desktop.
Save rendorHaevyn/914d3156cd72c135df7308fac3a5b104 to your computer and use it in GitHub Desktop.
Git Commands

Git How To

Initialise repo per the following link:

Publishing a new repo to GitHub from VS Code

Set up Git repo on local machine:

  1. Create your new repository on GitHub. Do not add README, license or gitignore for now. Note the “remote repository URL”, which you will need in step 7 instead of URL.
  2. Open terminal
  3. Initialize a new Git repository: git init
  4. Stage all your files before first commit: git add .
  5. Commit the files: git commit -m “Project started”
  6. Add remote repository: git remote add origin {**URL**}
  7. Verify the remote repository: git remote -v
  8. Push the first changes: git push -u origin master

Action (8) basically says push to origin (remote repo) the diff from master (local repo).

And to update after a change to .gitignore:

  1. run Add, Commit, Push
  2. Remove cache from index (staging area): git rm -r --cached .
  3. run Add, Commit, Push

Additional valuable commands

Visit learngitbranching.js.org to learn how to git

  • View reflog and history log, such as: git log -5 and git reflog -5
  • Branches are essentially pointers to a specific commit, and can be created by: git checkout -b "branchName" or doing both the following: git branch "branchName", git checkout branchName
  • Merging branches to master: merge commits and changes onto target branch, preserves history, better merge conflicts, easy to undo, can be messy (graphically): git checkout master, git merge branchName
  • Rebasing branches to master: moves the commits and changes onto the target branch, a clean history, readable graph, harder to undo: in one command - git rebase <CommitTo> <CommitFrom>, or in two commands - git checkout <CommitFrom>, git rebase <CommitTo>
  • View the diff between branches: git diff <<branch1>> <<branch2>>
  • Graphically depict branch history with: git show-branch -a
  • HEAD represents the commit that git is working off, which is moved using the git checkout <<pointer>> command. To work off a specific commit (like a branch name commit pointer): git checkout <<commit_tag>>
  • We can use the git branch pointer using ~n to move n number commits upstream: git checkout HEAD~4, git checkout master~3, git checkout branchName~1 etc...
  • To force-move a pointer to a different commit: git branch -f <<branchName>> <<pointer>>, ie: git branch -f master HEAD~3 will force the master commit pointer upstream 3 levels relative to HEAD
  • A reset on a branch will work locally to move upstream of the commit we want removed: git reset HEAD~1. However, these changes are not pushed / shared with remote streams.
  • A revert on a branch will work remotely to move upstream of the commit we want removed, which it does by creating a new commit that completely reverses all preceding changes, thus, preserving history: git revert HEAD
  • We can cherry-pick to apply the selected commits to the currently checked-out branch: git cherry-pick <Commit1> <Commit2> <...>
  • We can also do some crazy shit by using interactive rebase to pick and squash and do other stuff: git rebase -i <Commit1>
  • Using tag we can create a permanent pointer to a commit, like a version release: git tag AlphaVer <Commit1>
  • We can determine where we are relative to tags by using: git describe --tags <CommitName>; output might be like: alphaDemo-4-g2e087f6 (-<# Commits upstream>-g)

git config

git config –global user.name "[name]"
git config –global user.email "[email address]"
git config --global color.ui true

This sets up default user values
NOTE: Setup and use a personal access token for your password

git - repository setup

git init [repository name] - Initialise a new Git repository
git add . - Stage all files prior to commit
git commit -m "Initial Commit" - Commit files to the local repo
git remote add origin <<url>> - add Remote repository link
git push -u origin master - Push the first changes
git status - check the git status

git log & reflog & shortlog

git log --oneline --graph --decorate - Show commit history graphically
git reflog -5 --stat - Show stats for last 5 commits with statistics
git log -p --color-words - Show code changes with upstream commit, colour changes
git log --grep FOOBAR - Show commit messages containing string
git log -S FOOBAR - Show commit lines containing string
git shortlog -nse --author=<<name>> --since="1 year ago" - Show total number of commits by an author in the last year

git show

git show 290bfa2 --color-words - Show changes in a commit, colouring words
git show <<branch_name>> --color-words - Show changes in a branch, colouring words git show aa3332a:README.md --color-words - Show file from a different state

git checkout

git checkout <<branch_name>> - Switch working directly to a different reference or branch
git checkout -b - Switch to a different branch and create new branch

git branch

git branch -a - List all branches, including remote

git remote

git remote -v - Check remote details
git remote set-url origin ssh://git@newserver.com/repo.git - Change upstream url of the repository
git remote add new ssh://git@alt.newserver.com/repo.git - Add new remote ssh upstream

  • Using the new remote git fetch new
    git rebase new/master
    git push new master

git - changes in workspace state

git status
git status -s - A condensed status
git diff origin/main - Changes compared to upstream
git diff --cached - Diff of the staging area
git diff -w - Ignore whitespace changes in diff

git add

git add <<file_name>> - Add a changed filename to staging
git add -u src/ - Add all changed files in a directory to staging

git blame

git blame README.md - Check who changed the file
git blame 290bfa2^ README.md - An earlier revision

git stash

git stash - Stash all of the changes
git stash apply - Apply back all of the changes to the working directory

git - rewrite history

git revert <<reference>> - Revert changes back to prior reference, and retain history of changes
git reset --hard HEAD^ - Remove most recent commit and changes and delete history
git reset --hard HEAD~3 - Remove last 3 commits and changes and delete history
git reset --hard origin/main - Reset the state to upsteam
git commit --amend - Edit the commit last message

git rebase

git rebase -i HEAD~5

pick 7b07b03 track/manage size hints for zoomap child objects
pick 71a85b7 update winlist ui when using directional selection
pick bbd4d2f force changed when adding keyboards
pick a424542 disable emotion_shutdown during shutd own procedure

  • Commands:
    • p, pick = use commit
    • r, reword = use commit, but edit the commit message
    • e, edit = use commit, but stop for amending
    • s, squash = use commit, but meld into previous commit
    • f, fixup = like "squash", but discard this commit's log message
    • x, exec = run command (the rest of the line) using shell
    • d, drop = remove commit
  • Note:
    • These lines can be re-ordered; executed from top to bottom.
    • If you remove a line here THAT COMMIT WILL BE LOST.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment