Skip to content

Instantly share code, notes, and snippets.

@edcote
Last active September 19, 2018 18:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save edcote/4c6618de8c1811e277fae33430ef5e23 to your computer and use it in GitHub Desktop.
Save edcote/4c6618de8c1811e277fae33430ef5e23 to your computer and use it in GitHub Desktop.
GIT Notes

GIT

Getting started

  • Clone repository

    • Upload your public key to Bitbucket/GitHub
    • Clone using command: git clone git@bitbucket.org:<username>/<repo>.git
  • Pull from master branch

    • Use command git pull

Basic commands

  • File operations

      git add [..]    # Add file(s)
      git commit [..] # Commit file(s)
      git rm [..]     # Remove file(s)
      git mv [..]     # Move file(s)
    
  • Commit file(s) to local repo: git commit [..]

  • Push local repo to master branch: git push origin master

  • Clean untracked files: git clean -n

Submodules

  • To add/initialize submodules:

      git submodule add [link to repo] [local path]
      git submodule update --init --recursive [local path]
    
  • To remove submodules

    1. Delete the relevant section from the .gitmodules file. The section would look similar to:

       [submodule "vendor"]
           path = vendor
           url = git://github.com/some-user/some-repo.git
      
    2. Stage the .gitmodules changes via command line using: git add .gitmodules

    3. Delete the relevant section from .git/config, which will look like:

       [submodule "vendor"]
           url = git://github.com/some-user/some-repo.git
      
    4. Run git rm --cached path/to/submodule

    5. Run rm -rf .git/modules/submodule_name

    6. Commit the change

    7. Delete the now untracked submodule files rm -rf path/to/submodule

  • Reset submodule

      git submodule deinit -f .
      git submodule update --init
    
  • Update submodule to master branch

      cd my_submodule
      git checkout master
      git pull
      commit=`git rev-parse --short HEAD` # save the commit id
      cd ..
      git commit my_submodule -m "reference newer version ($commit) of submodule my_submodule"
      git submodule update --init --recursive my_submodule
    
  • List all changed files including those in the sumodules

      git submodule foreach --recursive git diff --name-status
    

Version and log info

  • How to find commit ID for submodules, few options:

    • Short commit ID: git rev-parse --short HEAD
    • Long commit ID: git ls-tree master
  • Show all branches, commits that are not remotes: git log --branches --not --remotes

Branching

  • To determine which branch am I on: git status
  • List files that differ from master: git diff --name-only master
  • Compare differences between branches: git diff master..branchname
  • List all branches: git branch -a
  • Rename branch, while on branch: git branch -m new-branch-name
  • Checkout local branch git checkout <name>
  • List remote branches: git branch -r
  • List all origins: git remote -v
  • Checkout remote branch git checkout -b <branch> origin <branch>
  • To switch back to master branch: git checkout master
  • Delete branch: git branch -D [..]

Working on a local branch of a forked repo. Tricky

git checkout master
git pull origin master (pull from remote master)
git checkout my_branch
git pull origin master (pull from local master?)
  • Update branch to master: git merge master

  • Steps to sync a forked branch: https://help.github.com/articles/syncing-a-fork/

    • Configure a remote that points to the upstream repository in Git: https://help.github.com/articles/configuring-a-remote-for-a-fork/

      • List current remotes: git remote -v
      • Specify new remote upstream: git remote add upstream https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git
      • Verify current remotes: git remote -v
    • Merge fork master with upstream master

      • Checkout master branch: git checkout master
      • Merge with upstream master git merge upstream/master (or git pull upstream master)?
    • Update local branch with fork master

      • git checkout devel-branch
      • git merge master

Gerrit

https://www.cheatography.com/oisch/cheat-sheets/hmi-git-gerrit/

  • Why refs/for/master?

See: https://stackoverflow.com/questions/10461214/why-is-git-push-gerrit-headrefs-for-master-used-instead-of-git-push-origin-mast

git push origin HEAD:refs/for/<BRANCH>

This pushes your changes to the staging area. It doesn't actually have a branch called ; it lies to the git client.

  • Checkout: Fetches the latest changes. You should already have this repo downloaded. It does not merge those new changes but makes your working directory reflect them. You can merge them at your leisure later.

  • Pull: Fetches the changes AND merges them into the local branch of the same name.

  • Cherry-pick: Fetches the commit and plays it on top of the current local branch, thus creating an entirely new commit which happens to have same changes as the one it fetched.

  • Tips
  • Reset local to head
  • Cherry pick commits
  • Rebase
  • Push
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment