Skip to content

Latest commit

 

History

History
117 lines (90 loc) · 4.43 KB

git.md

File metadata and controls

117 lines (90 loc) · 4.43 KB

GIT

Rename branch locally and remotely

git branch -m <old-name> <new-name>
git push origin --delete <old-name>
git push -u origin <new-name>

Setup a repository and remote

git init                          # initialize repo
git clone --bare /path/to/repo    # clone a bare repo
git remote add origin <repo_url>  # setup remote repo
git push -u origin master         # push upstream from local to remote repo

Add files

git add .             # add files in current folder to repo
git add -A            # add all
git add -u            # add modified files

Commit

git commit -m "abc"   # commit with a message
git commit --amend -m "update note"   # edit the previous commit message

git add forgotten-file.txt
git commit --amend --no-edit          # amend previous commit to include a file you forgot

Revert/reset

git reset HEAD <filename>    # unstage a file that was added
git reset HEAD               # unstage everything added
git revert HEAD              # revert back to the previous commit without erasing subsequent work
git revert laf452d           # revert back to previous commit

Remove files

git update-index --assume-unchanged <file>    # untrack files that are already in repo: best way [2]
git rm --cached <file>                        # untrack files that are already in repo: other way
git rm -r --cached <folder>                   # untrack files that are already in repo: other way

Delete branches

git branch -d branchname          # delete branch locally
git push -d origin branchname     # push deleted branch to remote
git fetch --prune                 # prune deleted branches (--prune = -p)

Diff files and branches

git diff branchA branchB -- file.py            # same file but different branches [3]
git diff branchB file.py                       # same file, different branches (if you are already in branchA)
git diff develop:foo.txt master:bar.txt        # Different files, different branches
git diff branchA branchB -- folder             # diff between folders
git diff --compact-summary branchB -- folder   # diff summary between folder between current branch and other branch
git diff ambiguous_name --                     #  Treat ambiguous name as a branch name
git diff -- ambiguous_name                     #  Treat ambiguous name as a file name
git diff --ignore-cr-at-eol --word-diff-regex="[^[:space:],]+"    # Diffs between words only
git diff --staged      # show differences in the staged files

Show file contents

git show --pretty="" --name-only 62c856a         # Show files in an old commit:
git show <commit>:path/to/file                   # Look at file in old commite
git log --all --full-history -- <path-to-file>   # Find out when file got deleted [1]
git log -p bar                                   # show history of a file 

Configure user details

git config --global user.name "user_name"
git config --global user.email "[email protected]"

RESOURCES:

[1] https://stackoverflow.com/questions/7203515/git-how-to-find-a-deleted-file-in-the-project-commit-history [2] https://stackoverflow.com/questions/1274057/how-to-make-git-forget-about-a-file-that-was-tracked-but-is-now-in-gitignore [3] https://stackoverflow.com/questions/38664421/how-do-i-call-git-diff-on-the-same-file-between-2-different-local-branches [4] https://clubmate.fi/git-removing-files-from-the-staging-area-and-the-tree/

Configure line endings

GitHub Setup

  • Tokens are now required instead of account password
  • To create token go to https://github.com/settings/tokens
  • After cloning the repo and making changes, do git push
  • Enter username for username
  • Enter the token for password