Notas

Notas, scripts, algoritmos de uso diario y documentación de algunos proyectos.

View on GitHub
8 March 2021

Git

by Juan Manuel González Garzón

Git

I. Managing a change whitout git

diff -u filename.txt changes.txt > filename.diff
patch < filename.diff # Output: patching file filename.txt

# TODO: Learn to use diff -r

II. Git configuration

# List the current configuration
git config -l

# configure your Git user information
git config --global user.name "Name"
git config --global user.email "address@email-domain.com"
git config --global color.ui true
git config --global core.editor "vim"
git config --global commit.template ~/.git_commit_msg.txt

A. Commit message template

# If applied, this commit will...            ...|
Commit title, Summarize changes in around 50 char

# Why is this change needed? in around 72 characters or less       ...|
Prior to this change,
# How does it address the issue?                                   ...|
This change
# Provide links to any relevant tickets, articles or other resources .|
# Example 1: Issue in the same repository (KEYWORD #ISSUE-NUMBER)  ...|
Closes #10
# Example 2: Multiple issues (Use full syntax for each issue)      ...|
Resolves #10, resolves #123, resolves octo-org/octo-repo#100
# ====================================================================|

Reference commit style.

III. Setup a workspace

# Clone the repository to your local machine.
git clone  <repo-url>

# Create a local repository
git init <project-name>

# Add a remote repository (Usefull for when ypu create the local repository)
git remote add origin <remote-repo-url>

# Print info of the remote repositories.
git remote -v

# Show the detail or a remote repository
git remote show origin

# Show remote branches
git branch -r

# Create a new branch and switches to it.
#-- [-b]: Create a new branch.
git checkout -b <brach-name>

# Switch branches
git switch main

IV. Git working areas

Git working areas git working areas. (n.d.). Google Coursera Curse Introduction-Git-Github. Retrieved July 14, 2021, from https://www.coursera.org/learn/introduction-git-github/

V. Show the changes information

# Show current status
git status

# Show the commits log trail
git log
git log - p # Produces patch text
git log --stat # Show some stats of the commit.
git log --all --decorate --oneline --graph

#Shows various objects
git show xxxxxxxxxxxx-commit-id-xxxxxxxxxxxx

# Show the diferences
git diff
git diff --staged

VI. Manage a workflow

# Add a file to your staging area
git add <file... or dir...>
# Allows a user to interactively review patches to add to the current commit
git add -p

 # Reverts changes to modified files before they are staged.
git checkout <file... or dir...>

# Resetting our changes to whatever's in the current snapshot.
git reset HEAD <file... or dir...>

# Remove a file from the staging area
git rm <file... or dir...>

# Rename a file/folder in the repository
git mv <file... or dir...> <file... or dir...>

# Commit files, save a bunch of changes.
git commit
git commit -a # Stages files automatically
# Modify and add changes to the most recent commit.
# The better is on local commits and never on publics or remote commits
git commit --amend
git revert HEAD # Creates a new commit with inverse changes.
git revert xxxxxxxxxxxx-commit-id-xxxxxxxxxxxx

# Send the changes to the remote repository
# -u: --set-upstream
git push --set-upstream origin branch-name # In case of new branch

# Show all the branches
git branch

# Create a new branch
git branch new-feature

# Merge 2 branches
# --ff-only: Preserve the commits history.
git merge --ff-only branch-b
# Cancel a merge
git merge --abort

# Delete a branch
git branch -d branch-name

git push --delete origin branch-name

# Update the local repository
git remote show origin

git fetch

git rebase origin/master
# if needed: git add file-corrected.txt
# if needed: git rebase --continue

git log -p -1

git log origin/master

git log -p origin/master

git merge origin/master

# Move the current branch on top of the refactor branch
git rebase refactor-branch

# Pull remote changes
git pull

git checkout remote-branch

Send patches as emails

# TODO: Lean anbout git-send-email
git send-email #Send a collection of patches as emails

Sign your commits

gpg --default-new-key-algo rsa4096 --gen-key

gpg --list-secret-keys --keyid-format=long
# /home/exampleuser/.gnupg/secring.gpg
# ------------------------------------
# sec   4096R/3AA5C34371567BD2 2016-03-10 [expires: 2017-03-10]
# uid                          Hubot <exampleuser@example.com>
# ssb   4096R/4BB6D45482678BE3 2016-03-10


git config user.signingkey 3AA5C34371567BD2

A. Common scenarios

1. Change branch after a commit is already made

Moving to an existing branch

git checkout existingbranch
git merge master
git checkout master
git reset --hard HEAD~3 # Go back 3 commits. You *will* lose uncommitted work.
git checkout existingbranch

Moving to a new branch

git branch newbranch      # Create a new branch, saving the desired commits
git reset --hard HEAD~3   # Move master back by 3 commits (Make sure you know how many commits you need to go back)
git checkout newbranch    # Go to the new branch that still has the desired commits

VII. Github Issues

Linking a pull request to an issue using a keyword You can link a pull request to an issue by using a supported keyword in the pull request’s description or in a commit message (please note that the pull request must be on the default branch).

The syntax for closing keywords depends on whether the issue is in the same repository as the pull request.

Linked issue Syntax Example
Issue in the same repository KEYWORD #ISSUE-NUMBER Closes #10
Issue in a different repository KEYWORD OWNER/REPOSITORY#ISSUE-NUMBER Fixes octo-org/octo-repo#100
Multiple issues Use full syntax for each issue Resolves #10, resolves #123, resolves octo-org/octo-repo#100

TODO: Analize history

# TODO: Lean anbout git bisect
git bisect

# TODO: Lean anbout git blame
git blame
sequenceDiagram participant Alice participant Bob Alice->>John: Hello John, how are you? loop Healthcheck John->>John: Fight against hypochondria end Note right of John: Rational thoughts
prevail! John-->>Alice: Great! John->>Bob: How about you? Bob-->>John: Jolly good!
graph TD; A-->B; A-->C; B-->D; C-->D;
graph TD A[Client] --> B[Load Balancer] B --> C[Server1] B --> D[Server2]
graph TD A[Client] -->|tcp_123| B(Load Balancer) B -->|tcp_456| C[Server1] B -->|tcp_456| D[Server2]
gantt dateFormat YYYY-MM-DD title Adding GANTT diagram to mermaid excludes weekdays 2014-01-10 section A section Completed task :done, des1, 2014-01-06,2014-01-08 Active task :active, des2, 2014-01-09, 3d Future task : des3, after des2, 5d Future task2 : des4, after des3, 5d
classDiagram Class01 <|-- AveryLongClass : Cool Class03 *-- Class04 Class05 o-- Class06 Class07 .. Class08 Class09 --> C2 : Where am i? Class09 --* C3 Class09 --|> Class07 Class07 : equals() Class07 : Object[] elementData Class01 : size() Class01 : int chimp Class01 : int gorilla Class08 <--> C2: Cool label
gitGraph: options { "nodeSpacing": 150, "nodeRadius": 10 } end commit branch newbranch checkout newbranch commit commit checkout master commit commit merge newbranch
erDiagram CUSTOMER ||--o{ ORDER : places ORDER ||--|{ LINE-ITEM : contains CUSTOMER }|..|{ DELIVERY-ADDRESS : uses
journey title My working day section Go to work Make tea: 5: Me Go upstairs: 3: Me Do work: 1: Me, Cat section Go home Go downstairs: 5: Me Sit down: 5: Me
pie title Key elements in Product X "Calcium" : 42.96 "Potassium" : 50.05 "Magnesium" : 10.01 "Iron" : 5

Creditos y referencias

Git icons created by pictogramer - Flaticon

tags: Version - Control - Systems