Skip to content

Latest commit

 

History

History
136 lines (114 loc) · 6.03 KB

GitCheats.md

File metadata and controls

136 lines (114 loc) · 6.03 KB

Git cheat-sheet.

A few examples to illustrate use of git on the command-line.

PLEASE READ CAREFULLY the CONTRIBUTING.md file in this directory. You must follow the specified flow to contribute to this repository.

If you have a suggestion to improve this document please either submit a pull-request, open an issue or email [email protected].

Useful Conventions:

  1. Place all your working copies in easy-to-find directories. A suggested nominclature is:
    $HOME/GitHubRepos/<GitHub_Account>/<Repository/><Branch>
  2. Use unique and easily recognizable names for your branches. Suggested nominclatures are:
    <org>_<userid>_yyyymmdd (e.g ohw_mike_20191120)
    or:
    <org>_<userid>_issue_<num> (e.g ohw_mike_issue_239)

Examples:

  • /home/mike/GitHubRepos/openhwgroup/core-v-verif/master
  • /data/mike/GitHubRepos/openhwgroup/core-v-verif/ohw_mike_20191204
  • /wrk/greg/GitHubRepos/openhwgroup/cv32e40p/master
  • /home/mike/GitHubRepos/MikeOpenHWGroup/core-v-docs/ohw_mike_issue_123

Example Use-cases

"$" is the prompt. "#" is a comment line. ">" is git output to stdout.

Clone from the head of a Repo's master branch on the command-line.

Note that this is not a typical use-case (work on a branch instead).
Place all your working copies in easy-to-find directory:
e.g. $HOME/GitHubRepos/<GitHub_Account>/<Repository>/<branch>
$ cd $HOME/GitHubRepos/openhwgroup/cv32e40p
$ git clone --recursive https://github.com/openhwgroup/cv32e40p
$ gvim Makefile #...edit file(s)...
$ git commit -m 'Added support for dsim' Makefile
# First time users might be asked to update their info...
$ git config --global --edit
$ git commit --amend --reset-author

Make a branch on the command-line and switch to it

$ cd $HOME/GitHubRepos/openhwgroup/cv32e40p
$ git clone --recursive https://github.com/openhwgroup/cv32e40p ohw_mike_20191121
$ git branch ohw_mike_20191121
$ git checkout ohw_mike_20191121
...or...
$ git checkout -b ohw_mike_20191121
# ...edit file(s)...
# push files back to the branch
$ git status # check to see what's different
$ git remote -v # check to ensure remote is the branch you want
$ git commit -m 'Useful commit message'
$ git push --set-upstream origin ohw_mike_20191122

Clone directly from a branch to a directory named for that branch

$ cd $HOME/GitHubRepos/openhwgroup/cv32e40p
$ git clone --recursive -b ohw_mike_20191122 https://github.com/openhwgroup/cv32e40p ohw_mike_20191122

Get the short version of the hash of your clone

$ git log --pretty=format:'%h' -n 1

Sync a branch to the master (same repo)

$ cd $HOME/GitHubRepos/<GitHub_Account>/
$ git checkout master
$ git pull
$ git checkout
$ git merge master
$ git push --set-upstream origin

Sync a forked repo to make it up-to-date with its upstream repo

The following assumes you have previously created a fork of
https://github.com/openhwgroup/core-v-docs
to
https://github.com/MikeOpenHWGroup/core-v-docs

$ cd GitHubRepos/MikeOpenHWGroup/core-v-docs
$ git clone https://github.com/MikeOpenHWGroup/core-v-docs.git master
$ cd master
$ git remote -v
> origin https://github.com/MikeOpenHWGroup/core-v-docs.git (fetch)
> origin https://github.com/MikeOpenHWGroup/core-v-docs.git (push)
$ git remote add upstream https://github.com/openhwgroup/core-v-docs.git
$ git remote -v
> origin https://github.com/MikeOpenHWGroup/core-v-docs.git (fetch)
> origin https://github.com/MikeOpenHWGroup/core-v-docs.git (push)
> upstream https://github.com/openhwgroup/core-v-docs.git (fetch)
> upstream https://github.com/openhwgroup/core-v-docs.git (push)
$ git fetch upstream
$ git checkout master
$ git merge upstream/master
$ git push --set-upstream origin master

Importing a new upstream branch onto your fork

  • Make sure you've pulled the new upstream branch into your local repo:
  • First, ensure your working tree is clean (commit/stash/revert any changes), then:

$ git fetch upstream to retrieve the new upstream branch

  • Create and switch to a local version of the new upstream branch (newbranch):

$ git checkout -b newbranch upstream/newbranch

  • When you're ready to push the new branch to origin:

$ git push -u origin newbranch
The -u switch sets up tracking to the specified remote (in this example, origin).

Using ssh (need to set-up ssh keys first)

# git remote set-url origin [email protected]:username/your-repository.git
$ git clone [email protected]:openhwgroup/core-v-verif.git master

Metrics CI Cheat Sheet

Add GitLab Metrics remote

$ git remote add metrics [email protected]:cv32e40p_verif/cv32e40p_verif.git

Check to see if you have the Metrics remote added

$ git remote -v
> metrics [email protected]:cv32e40p_verif/cv32e40p_verif.git (fetch)
> metrics [email protected]:cv32e40p_verif/cv32e40p_verif.git (push)
> origin https://github.com/openhwgroup/core-v-verif (fetch)
> origin https://github.com/openhwgroup/core-v-verif (push)

Rebasing a previous commit

# https://stackoverflow.com/questions/3042437/how-to-change-the-commit-author-for-one-specific-commit
# https://docs.github.com/en/github/getting-started-with-github/about-git-rebase#an-example-of-using-git-rebase
$ cd <working_dir>
$ git rebase --interactive HEAD~7
# This pops an editor session of the last seven commits
# In this example, change two commits from "pick" to "edit"
# Quit the editors and then do the following at the command-line...
$ git commit --amend --author="Mike Thompson [email protected]" --no-edit
$ git rebase --continue
$ git commit --amend --author="Jean-Roch Coulon [email protected]" --no-edit
$ git rebase --continue
$ git remote -v
$ git push -f