somascope
4/5/2018 - 8:21 PM

Git Notes

Cheat Sheets

https://github.github.com/training-kit/downloads/github-git-cheat-sheet.pdf

Configure git

These are not credentials, they're just who gets credit for commits.

git config --global user.name "Your Name"

git config --global user.email "youremail@example.com"

If creating a brand new project folder using Git

git init projectname

cd projectname

Adding a remote

"origin" is the name most commonly used for a remote

git remote add origin https://github.com/somascope/some-github-remote.git

git remote set-url https://github.com/somascope/some-new-url.git

Add an add'l remote to be able to get updates from what you forked:

(can be named anything, but 'upstream' is common)

git remote add upstream https://github.com/original-repo-you-cloned.git

Find out basic info

git config --list (press 'q' to exit)

git config user.name

git config user.email

git remote -v to view remote addresses

Clone

Clone a repo

git clone [url]

Clone a repo into a specific folder on your computer

git clone [url] someFolderName

Finding info about your local repo

Show all commits starting with the newest

git log

List new (intracked) and modified files to be committed

git status

Show file differences not yet staged

git diff

Doing work - use branching

Applying your changes

Add files (stage files)

git add . (stages new and modified files, without deleted files)

git add -A (stages ALL)

git add -u (stages modified and deleted, without new files)

Commit and push changes

git commit -m "Your descriptive message"

git push origin master (push local branch master to remote branch master)

Undoing local changes

If deleted local files but want to get them from the remote repo

git checkout theDirectory

Undoing a specific modified file

When you edited a file and save/close it, have not added/staged the file, and want to go back to before your edits.

git checkout -- some-file.js

Undoing all recent modified files

git checkout -- .

Removing an added file from staging

When you added a file and now want to unstage it.

git reset some-file.js

Delete files or folder

git rm file-to-delete.js

git rm -r some/folder/toDelete

Delete files outside of git

(delete the files)

git add -u

git commit -m "Deleted xyz files manually"

Difference between files

git diff (sha-id-one) (sha-id-two) -- filename.ext

i.e.: git diff da9dedee e3f28878 -- src/components/ConstrainedControl.vue

Difference between commits

Get a list of changed files since a previous commit

git diff --name-only <starting SHA> HEAD

git diff --name-only c2f85aa974367f79d8b2036d014916490cc63aae HEAD

Tags

git tag v0.9

git tag -a v0.9 -m "Scrolling layout"

checkout tag v0.9

git push origin --tags

Other

Syncing: https://www.atlassian.com/git/tutorials/syncing/git-pull

Pull changes from a remote branch

Note that you cannot make changes directly on a remote branch. Hence, you need a copy of that branch. Say you wanted to copy the remote branch fix-failing-tests, here's how you would do it:

git checkout -b fix-failing-tests origin/fix-failing-tests

To get updates from the remote repo

git pull remote remote-bgranch

This is a combo of git fetch and git merge. it gets the updates from remote repository (git fetch) and immediately applies the latest changes in your local (git merge). This may cause conflits that oyu need to resolve manually.

git fetch

git remote -v (show remote details)

https://docs.gitlab.com/ce/gitlab-basics/start-using-git.html