github_notes - RicoJia/notes GitHub Wiki

Resources:

  1. Mando

Basic concepts

  • HEAD: where am I right now in the repo, or "the last commit in the current branch"
    • detached head means: now HEAD is pointing to a commit, instead of a branch works for sub_directory as well.
      • In detached state, you can make experimental changes, without affecting any branches.
    • good read

========================================================================

Sample Workflows

========================================================================

  • Pull Request, feature branch model

       - fork the repo on github.com
       - git clone the repo
       - git checkout -b NEW_BRANCH 
       - git add, git commit
        - ```git add``` doesn't add empty dirs
       - git push origin NEW_BRANCH   #you can see you've pushed to a new branch
       - comapre and make pull request, so in the pull request tab, you can see it. # the owner can merge 
    #the pull request 
  • Fix Bugs

      - git diff BRANCH1 BRANCH2
      - Then go to your branch, check out the <<<<< ==== >>>> marked lines, Edit them?
      - Add and commit
      - git checkout master
      - git merge some_branch --no-ff           #if no --no-ff, there will be no commits for pure addition. 
      - git branch -d devel 
      - git log --one line to simplify the output, see what commits there are
  • Undo undesired changes:

      # Before staging
      git stash							#temporarily store all changes somewhere else.
      or 
      git reset -- hard				    #Discard all local changes permanently
    • undo a commit: git revert <SHA>
  • if you hit git reset accidentally try git reflog

  • if develop is ahead, how to merge it into my branch? git checkout develop git pull #this only pulls from the same remote branch. git checkout feature git merge develop

  • Check changes you've made

     git checkout develop
     git pull
     git checkout FEATURE
     git diff develop FEATURE
    • sometimes, after git checkout, you may see M, D... Those are uncommited changes from the last branch. You can still see them, but won't after you commit.
    • If you see older branches, do git fetch -p
  • Request for review

    • Make sure your commit is a draft
    • Then, change it back to "ready for review"
    • you can change it back any time: just go under the reviewers - convert to draft
  • git rebase and squashing

    • Scenario: once you're going to merge it into develop branch
    • Workflows
       	gitk or git	log --oneline #to find which commits to squash
       	git rebase -i HEAD~2  #this is to pull out the most recent 2 commits, so you can find the last two
       	# A file will come up, change the newer commit from "pick" to "squash", close the file
       #the newer one should be close to the bottom
       # This is because a remote repo is more likely to have the older version 
       	# Another file comes up, use # to commment out the old commit messages. 
      

========================================================================

Other common scenarios

========================================================================

  1. remove changes

    git reset HEAD file (unstage staged but uncommited changes)
    git checkout -- file (remove all unstaged changes made to a file)   # -- means to a file, VERY IMPORTANT
    git restore --staged dir
    • git checkout BRANCH -- FILE_PATH: replace FILE_PATH on the current branch with the on BRANCH
    • -- File indicates what follows is a file
  2. clone a branch, or tag

  • git clone a branch, git clone -b <branch_name> ...
  • clone a tag:
    • You might find a version in tags. git clone -b <tag_name> --single-branch repo
  1. Create a new branch from an exisiting branch
  • git checkout -b NEW_BRANCH branch2?
  • or
      git branch NEW_BRANCH     #so please do git branch -a, not git branch a
      git checkout NEW_BRANCH
    
  1. rename a branch:
git branch -m <BRANCH_NAME>
  1. Undo the last changes
    • git reset --hard HEAD~1 remove the changes since last commit.
    • git reset --soft HEAD~1 (or SHA1) -- still keeps the changes. but as unstaged
    • git revert (TODO) ,adds the unstaged changes as a new commit? (yes, but found some changes were not undone. Need further investigation)
  • discard local changes
    git stash save --keep-index --include-untracked
    git stash drop
    
  • Go back to a previous commit, test.
    git checkout COMMIT_SHA1  // don't include ., else you will not enter a "detached state", and anything you do WILL be saved as a last commit
    #do tests
    
    • then come back without having any changes,
        # don't commit anything
        git checkout BRANCH_NAME
      
    • If you want to keep the changes, then make a branch
      git checkout -b NEW_BRANCH
      
  1. Start a new account:
    • Follow the github page steps closely, add your personal ssh
  • reset a url
    - git remote add NAME URL
    • E.g, git remote add origin [email protected]:ME495-EmbeddedSystems/homework-2-RicoJia.git
    • Use SSH instead of HTTPs so you always automatically push
  1. delete a branch
  • git push origin --delete BRANCH_NAME Delete a branch remotely
  • git branch -d BRANCH to locally delete branch, if not committed, fails
  • git branch -D BRANCH to locally delete branch force
  1. list all files
  • git ls-tree -r master --name-only
  1. see config, git config:
    • git config --global user.name "name"
    • git config --global user.email "email"
    • Technically, you can remap commands
      git config --global alias.st status #Remap status to st
      
  2. git cherry-pick SHA: 将一个commit 直接移植过来。(only the commit, nothing else)
  3. insert a commit before a previous one:
    git checkout -b temp PREVIOUS_COMMIT
    git commit 
    git rebase temp develop
    #fix some conflicts
    git add files_changed
    git rebase --continue
    
  4. Scenario: you want to update your branch from another branch:
    git pull origin FEATURE 
    
    • if pulling doesn't solve the problem, maybe you're change is already ahead of the branch!!
  5. Scenario: files exist on another branch, and you want to get them here
    git checkout FEATURE -- FILENAME
    

========================================================================

Update Repo

========================================================================

  1. git fetch + git merge allows you to choose which branch of local repo you want to update.

    • conflict: two person changing the same lines.
      • Make sure you remove all the conflict markers <<<<<<, else there will be problems.
    • git fetch
      • Get update from remote to local branch, including all commits, but this doesn't update anything
  2. git merge

    1. git merge
      • will merge try to merge the newest local commit, and the newest remote commit, and you gotta solve merging conflicts. Then, a new commit is created
      • go to the main branch, then merge feature branch into it $git checkout develop $ git merge feature
    2. git mergetool is a good tool to see merge conflicts.
    3. Merge two remote branches: ```bash git clone repo.git #you can download only one branch?? Well, check git branch -a to see all your options!! git checkout destination_branch #(could be a remote one, like origin/something, but just type the name after origin) git pull --allow_unrelated_histories repo.git branch_name commit all changes and push
    4. ```git merge -no-ff BRANCH```
    - ```no-ff``` is no fast forward: if BRANCH is a newer branch, then instead of bring head to BRANCH, you go one step further and create a new commit on top of it
    
    
  3. git stash Should there be a merging conflict,

    • git stash: after git stash pop, you might get conflicts. to solve it, do git add
    • More comprehensively,
      git stash                                       #save your work aside for working on higher priority tasks. make sure everything is commited or stashed
      git stash list                                  #check out a list of stashes, what is stashed from which branch. 
      # you can see the stashes, then apply it. 
      git stash apply stash@{0}                       #you can save the changes back to your branch. Note: this step does not remove the stash
      git stash drop stash${1}                                   #to  dump stash here.
      git merge branchB                               #on branch B, merging branch A
    • git stash show, see files in most recent stash
    • git stash show -p see diff
    • git stash save -m "msg" just like git stash
  4. git pull pull all branches

    • git pull origin BRANCH
    • git push is equivalent to git fetch REMOTE + git merge
      • You can also git pull --rebase, which shoves the local commits right after the remote commits. rebase is good for keeping a linear history
      • But now I prefer
         	git checkout develop
         	git pull 
         	git checkout feature
         	git rebase develop
         	#solve conflict
        
  5. git push

    • git push -u: -u is to make sure the branch on the remote host will be tracked, so git pull will work seamlessly.
    • git push origin --delete name, delete remote branch
  6. git add:

  git add . is dangerous
  git add -p file  # you can incrementally add files 
- git has a "buffer" called stage, which is the main difference from SVN. git add is "staging"
- git commit -am "": a is to add all tracked files to stage.
- Stage the new file. nothing being added right now but it's telling git that it's going to add something for commit
- ```git add -A```: **stage all files/deleted files, better than git add.**	
  1. git commit -m "message"

    • commit with a message. BE SURE IT's IN YOUR BRANCH!!
  2. Git Submodules - This has bee n a pain

    • git clone recursive clones the module and all submodules
    • Add git submodules:
    # 1 go to destination folder
    # 2. do this directly 
    git submodule add <remote_url>
    # 3. see list of submodules
    git submodule--helper list
    # VERY IMPORTANT:
    git add & git commit
    
    • update/download submodules: git submodule update --init --recursive
      • branch might have been switched
    • Tricky situation:
      1. say you are in ~/non_git_repo, and you have~/non_git_repo/git_repo
      • if you have ~/non_git_repo/git_repo/bash_file that does git submodule update --init --recursive
      • do ./git_repo/bash_file will fail, because you can't run git submodule in a non-git root
      1. How to remove a submodule
        Delete the relevant section from the .gitmodules file.
        Stage the .gitmodules changes git add .gitmodules
        Delete the relevant section from .git/config. This caches the older submodule HEAD
        Run git rm --cached path_to_submodule (no trailing slash).
        Run rm -rf .git/modules/path_to_submodule (no trailing slash).
        Commit git commit -m "Removed submodule "
        Delete the now untracked submodule files rm -rf path_to_submodule
        
        • When you test, If you have submodule inside a submodule
          • you must make sure you are on the right branch in every repo. Otherwise, you might see
            fatal: remote error: upload-pack: not our ref 578dd0a0ac8fe1104e9cafee6f6670d4561baddc
            
          • that means the cached submodule in ~/root/.git/config has been removed, but your submodule has not been updated. So delete your submodules and start over
            fatal: 'git status --porcelain=2' failed in submodule nonros/scheduler
            
    • git submodule sync
      1. submodule host url changes
      2. update local .gitmodules for the url
      3. run git submodule sync to update the local configs
  3. To download a concrete branch, first git checkout -b, then git pull

Status & Diff

  • git status

    • check out files that are added. It will show which
  • git diff

    • See differences you have made.
      • git diff BRANCH1 BRANCH2 Compare two branches
    • git diff of a certain path git diff develop feature PATH
    • git diff ignoring white space git diff -w develop feature
    • checking diff between current commit and last commit git diff HEAD^ HEAD, ^ means one prior commit
    • git diff file: see what's changed
    • git --no-pager diff --name-status origin/develop will show on the screen what pkgs have been changed
  • git log : show commit history

    • git log --format="%aN" -- FILE_NAME shows the authors of a FILE_NAME
  • git show FILE_NAME: show the file content; git show just shows files changed from the previous commits!!

========================================================================

Branching and Tagging

========================================================================

  1. git branch

    1. git branch -a
      • check all existing branches in your repo directory;
      • * means current branch; HEAD is the pointer pointing to your current branch
    2. git branch -d branch_name
      • deletes a local branch
    3. git branch -m rico/Refactored_REST_endpoint, change name of branch
  2. git tag: a pointer pointing to a commit 2. git tag v1.0 create a new tag - git tag v0.9 f52c633 create a new tag on this commit - git tag -a v0.1 -m "version 0.1 released" 1094adb add some msg 3. git tag to see all tags - git show TAG: see the tag commit info 4. git tag -d v0.1 delete tag - git push origin :refs/tags/v0.1 delete a remote tag, you don't have to delete the local tag 5. git push origin v0.1 push this tag to the remote machine - git push origin --tags push all tags - git push origin v0.1: so you can push a tag successfully

Jimmy's Github Rules

  1. Create a branch off of develop
    • rico/JIRA_ID_name
    • You can have multiple PRs under the same JIRA Ticket
  2. Github User name is First name + Last name
    • CHANGES.txt is generated this way
  3. Mark your PR as draft
  4. Make sure your tests have been passed! on ocean_builds
  5. Add tests if you add a new feature
  6. Do not run git add .
    • git commit -am "" Don't use that, use git commit -m
  7. Do not check in temp files. Use .gitignore for that
  8. Post on ocean_pr, even for comments addressed.
  9. Github Best Practices
    • PR should not live > 1 wk.
    • First work on your own branch. When you're close to finishing it, open a draft PR.
    • [QA-529] some fixes ... (informative title)
    • post a link to the Channel once code ready for review
    • Delete your branch

========================================================================

Misc

========================================================================

  1. How to write .gitignore?

    • * matches any char in file name, ** matches dirs before it
    • if you want to exclude a file in the current directory: /CMakeLists.txt
    • or a directory: /dist . / means directory, and is relative to where .gitignore resides.
    • git ignore
      *.class
      
      # 不排除.gitignore和App.class:
      !.gitignore   #so you can't do git add -f
      !App.class
      
    • git add -f App.class override gitignore
  2. Continuous Integration and Continuous Delivery

    • Continuous Integration
      • When you have a conflict, you want to find it quickly, even if they're in the feature branches.
      • CI will continuously build their versions(?), test the build (the tests are stored in a different locations) (What test?), and and Inform them (?)
  3. a bare git repo has only .git, with head, config, etc. mirror push to your own repo: git push --mirror https://github.com/<your_github_handle>/lab-exercises.git

    • Question: can you use ssh instead of https?
  4. semantic versioning: V 0.1.2 - the first major version, with 1st generation features, and 2 bug fixes

  5. if you see insufficient permission for adding an object, go change the file permission

  6. Github.io:

    1. website: go to the root directory with "_site" directory, first jekyll build, then jekyll serve
  7. adding another account on your computer

  8. git bisect to find the bad commit that was causing trouble

    1. git bisect bad HEAD -> git bisect good <KNOWN_GOOD_COMMIT>
    2. Then git will jump to the middle commit
    3. Keep doing this, until we hit the bad apple
    4. git bisect reset. when you're done to jump back to HEAD

========================================================================

Gitlab

========================================================================

  1. Mirrored repos from github
  2. Show markdown
    • either do $`a^2+b^2`$, or do:
          ```math
          a^2 + b^2
          ```
      

========================================================================

References

========================================================================

  1. render latex:
    • Render it, and copy the url from a drop down menu
⚠️ **GitHub.com Fallback** ⚠️