James's Ramblings

Git

Created: August 10, 2020
Command Description
git init Create a local repository in the current directory
git clone Clone a remote repository.
git add {FILE|GLOB} Tell git to track a file.
git commit Commit any changes.
git status Shows status of files. What has been changed, etc.
git branch Show which branch is being worked on locally.
git merge Merge branches.
git pull Pull changes from the remote repository to the local machine.
git push Updates the remote repository with anything committed locally.
git clean Removed untracked files and directories. Use carefully.
  • Git internal data is stored in .git/.

Set Up a New Repository

mkdir REPO_NAME
cd REPO_NAME
echo "SOMETHING" > README.md
git init                       
git add FILES
git commit -m "MESSAGE"       # a local repo now exists
git remote add origin git@github.com:JamesLochhead/SOMETHING.git # needs to exist
# now synced with GitHub
git push -u origin master

.gitignore

  • A file within the top-level of the git directory containing globs for files/directories that should be ignored. One line per pattern.

Semantic Versioning

  • Peformed using tags:
    git tag -a vX.Y.Z -m "MESSAGE"
    
  • To push tags upstream:
    git push --tags
    
  • The describe command can be used to get useful information about tags:
    git describe --tags --long
    
  • The number after the tag, between the two en dashes, represents the number of commits since the last tag.

Branching

  • To create a new branch:
    git checkout -b BRANCH_NAME
    
  • This is shorthand for:
    git branch BRANCH_NAME
    git checkout BRANCH_NAME
    
  • You cannot change branches with uncommitted changes.

Merge a Branch with Master

  • Commit changes before using checkout.
  • Change to master:
    git checkout master
    
  • Merge BRANCH with master:
    git merge BRANCH
    

View untracked files and directories

git clean -xdn
  • Use carefully. Without the n flag, it removes all untracked files (-x) and all untracked directories (-d).

Remove a file from git

From git and the local filesystem:

git rm FILE

From just git:

git rm --cache FILE

Check for changes in a Git remote

git fetch origin

See the commit hash of HEAD

git rev-parse HEAD

Reset to a specific commit

git reset --hard COMMIT
git reset --hard HEAD~2

Revert a file to HEAD

git checkout -- FILE

Checkout a file from a different branch

git checkout BRANCH -- FILE

Show differences between branches

git diff BRANCH1..BRANCH2

Show files that are different between branches

git diff --name-status BRANCH1..BRANCH2

Delete a local branch

git branch -d BRANCH

Delete a remote branch

git branch -d BRANCH
git push origin --delete BRANCH

Merge and squash

git merge --squash BRANCH