github_notes - RicoJia/notes GitHub Wiki
Resources:
- 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
- detached head means: now HEAD is pointing to a commit, instead of a branch works for sub_directory as well.
========================================================================
========================================================================
-
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>
- undo a commit:
-
if you hit
git reset accidentally
trygit 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.
========================================================================
========================================================================
-
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
: replaceFILE_PATH
on the current branch with the onBRANCH
-
-- File
indicates what follows is a file
-
-
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
- You might find a version in tags.
- 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
- rename a branch:
git branch -m <BRANCH_NAME>
-
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
- then come back without having any changes,
-
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
- E.g,
- 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
- list all files
git ls-tree -r master --name-only
- 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
-
git cherry-pick SHA
: 将一个commit 直接移植过来。(only the commit, nothing else) - 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
- 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!!
- Scenario: files exist on another branch, and you want to get them here
git checkout FEATURE -- FILENAME
========================================================================
========================================================================
-
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.
- Make sure you remove all the conflict markers
-
git fetch
- Get update from remote to local branch, including all commits, but this doesn't update anything
- conflict: two person changing the same lines.
-
git merge
-
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
- git mergetool is a good tool to see merge conflicts.
- 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
-
-
git stash
Should there be a merging conflict,- git stash: after
git stash pop
, you might get conflicts. to solve it, dogit 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
- git stash: after
-
git pull
pull all branchesgit 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
- You can also
-
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
-
-
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.**
-
git commit -m "message"
- commit with a message. BE SURE IT's IN YOUR BRANCH!!
-
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:
- 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 doesgit submodule update --init --recursive
- do
./git_repo/bash_file
will fail, because you can't rungit submodule
in a non-git root
- 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 overfatal: 'git status --porcelain=2' failed in submodule nonros/scheduler
- you must make sure you are on the right branch in every repo. Otherwise, you might see
- When you test, If you have submodule inside a submodule
- say you are in
- git submodule sync
- submodule host url changes
- update local
.gitmodules
for the url - run
git submodule sync
to update the local configs
-
-
To download a concrete branch, first
git checkout -b
, then git pull
-
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
- See differences you have made.
-
git log
: show commit history-
git log --format="%aN" -- FILE_NAME
shows the authors of aFILE_NAME
-
-
git show FILE_NAME
: show the file content;git show
just shows files changed from the previous commits!!
========================================================================
========================================================================
-
git branch
-
git branch -a
- check all existing branches in your repo directory;
- * means current branch; HEAD is the pointer pointing to your current branch
-
git branch -d branch_name
- deletes a local branch
-
git branch -m rico/Refactored_REST_endpoint
, change name of branch
-
-
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
- Create a branch off of develop
- rico/JIRA_ID_name
- You can have multiple PRs under the same JIRA Ticket
- Github User name is First name + Last name
- CHANGES.txt is generated this way
- Mark your PR as draft
- Make sure your tests have been passed! on
ocean_builds
- Add tests if you add a new feature
- Do not run
git add .
-
git commit -am ""
Don't use that, usegit commit -m
-
- Do not check in temp files. Use
.gitignore
for that - Post on ocean_pr, even for comments addressed.
- 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
========================================================================
========================================================================
-
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
-
-
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 (?)
- Continuous Integration
-
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?
-
semantic versioning: V 0.1.2 - the first major version, with 1st generation features, and 2 bug fixes
-
if you see
insufficient permission for adding an object
, go change the file permission -
Github.io:
- website: go to the root directory with "_site" directory, first
jekyll build
, thenjekyll serve
- website: go to the root directory with "_site" directory, first
-
git bisect
to find the bad commit that was causing trouble-
git bisect bad HEAD
->git bisect good <KNOWN_GOOD_COMMIT>
- Then git will jump to the middle commit
- Keep doing this, until we hit the bad apple
-
git bisect reset
. when you're done to jump back to HEAD
-
========================================================================
========================================================================
- Mirrored repos from github
-
Show markdown
- either do
$`a^2+b^2`$
, or do:```math a^2 + b^2 ```
- either do
========================================================================
========================================================================
-
render latex:
- Render it, and copy the url from a drop down menu