Git basic operations - ZephyrJW/learn_github GitHub Wiki

Git Repository setup

  • Create repository locally: git init
  • Clone existing repository: git clone [url]

File Status

  1. Untracked (?? in -s)
  2. Unmodified
  3. Modified ( M[not staged] or M [staged] in -s)
  4. Staged

ignore setting

  • .gitignore, just write file type or directory in this file.

Workflow

git status or git diff => git add -A => git commit -m "info"
Look up current state => cache files => stage files
git commit -a -m "info" to bypass git add

Removal

  • git rm -f filename: remove file from cache and disk
  • git rm --cached filename: remove file only from cache but keep it on disk
  • git rm \*~: need \ backslash

Rename

  • git mv file_from file_to

History(log)

  • git log [-p] [-n]: display in details what's changed, -n defines how many entries to display
  • git log --stat: display a concise statistical change
  • git log --pretty=format:"%h %s" --graph: format the log content and using graphical display

Undoing Things

Amend a commit

  • git commit --amend: forgot to add some files after commit. usage:
    $ git commit -m 'initial commit'  
    $ git add forgotten_file  
    $ git commit --amend

Unstage staged file

  • git reset HEAD <file>

Discard changes in working directory

  • git checkout -- <file>

Remember, anything that is committed in Git can almost always be recovered. However, anything you lose that was never committed is likely never to be seen again.

Working with Remotes

Showing remotes

  • git remote -v

Adding Remote Repositories

  • git remote add <shortname> <url>: add a remote repository from other college working on same project

Fetching and Pulling from Remotes

  • git fetch <remote_shortname>: fetch data from remote, without merging
  • git pull <remote_shortname>: automatically fetch and then merge that remote branch into your current branch

Pushing to your remote

  • git push <remote> <branch>: consistency with the remote is required

Inspecting a Remote

  • git remote show <remote>: list the URL for the remote as well as the tracking branch information

Renaming and Removing Remotes

  • git remote rename <old_shortname> <new_shortname>
  • git remote remove(or rm) <shortname>
⚠️ **GitHub.com Fallback** ⚠️