Git

From bibbleWiki
Revision as of 22:23, 13 November 2024 by Iwiseman (talk | contribs) (Rebasing)
Jump to navigation Jump to search

Helpful commands

Test

int main(int a, char* args[]) {
  // Test
  return 0;
}

Getting stuff from other branch

 git remote add live <other branch>
 git pull live master

Commit

 git commit -m "This is a great check in"
 git push

Checking out a branch

 git clone <address>
 git fetch origin
 git checkout <branch name>

Setting Credentials

 git config --global credential.helper store

Show non pushed changes

 git diff origin/master..HEAD

Put a cloned project into gitlab

git init
git remote set-url origin http://gitlab.com/bibble235/project.git
git commit -m "copy of project"
git push

Compare Branches

$ git diff branch1..branch2

Show Which Branch

git branch

Merging

This is how to merge from main to mybranch

// clone the source
git clone blahhhh
// checkout the branch
git checkout mybranch
// merge changes into main
git merge main
// Go into vs code
// Take the version you care about
git commit -m 'Merge code to main'
git push

View Deleted Files

# List all deleted files
git log --diff-filter D --pretty="format:" --name-only

# Find last commit
git log -n 1 --pretty=format:%H -- src/services/__test__/myFile.test.tsx

Setting up new Repo from CLI

 cd /path/to/repo
 # Make sure you need this line, maybe you did it wrong the first time 
 rm -rf .git
 git init
 git branch -m main
 git remote add origin https://gitlab.com/<user>/<repo_name>
 git add .
 git commit -m "first commit"
 git push -u origin main

Making a Branch for Change

git branch 123xx
git checkout 123xx
git add .
git commit -m "Blah"
git push

# Make a pull request

# If request rejected you can reset what you have
git reset ^HEAD

# Recommit your stuff
git add .
git commit -m "Blah"
git push

Fixing Merge Conflict on Pull Request when main has changed

When someone makes a change on main whilst you are working on a branch, you can run this on your branch and remove the offending file

git pull --no-rebase origin main

Merge Remote Feature Branch to Local Branch

This is when you are waiting for PR 30441 to be processed and you depend on their code. To put remote PR code onto your local branch

git merge origin/30441

July 2023

Update local Main Branch (main) to remote (main) branch

// Make sure you have not modified this prior to starting (unless you wanted to)
// In the local copy directory
git pull origin main
// Check all good git diff should be empty (unless you have made changes)
git diff

Update local non-Main Branch (DEV) from remote (main) branch

This is the way I currently do it

git fetch origin main
git merge origin/main

Brush Up

This is a quick refresh where I might redo this page

Config

This is where it lives and how to look from cli

# View
cat ~/.gitconfig

# List
git config --list

# Change default branch
git config --add --global  init.defaultBranch blakes7

We can set values locally for just the project we are in with

# List config
git const --list --local

# Set Values in Local
git config --add --local mysection.mykey myKeyValue1
git config --add --local mysection.mykey myKeyValue2

You can rename a local branch which might have master as the default branch and set the default with

git branch -m master main
git config --add --local  init.defaultBranch main

Switching branch

They recommend git switch but I always use git checkout

git switch branch2
# Same thing
git checkout branch2
git pull branch2

Cat File

This allows you to see the internals of git

# You only need maybe first four characters
git cat-file -p <hash>

# Example
git cat-file -p 223ab5b7
tree 927baa9ae05483c2446c482955cec508ed200120
parent ae62e2083838bfbd6f6b929db09430d08e62e9ef
author Bill Wiseman <bill.wiseman@ihc.org.nz> 1729131283 +1300
committer Bill Wiseman <bill.wiseman@ihc.org.nz> 1729131283 +1300

#30441 Fix|Added updating of error when no ServiceRate Found

Looking at the output we can see parent and tree. The parent will be the previous commit and the tree is a reference to the branch

Merging

Given a main branch with commits A, B, C and your branch with two commits D and E, we want to merge our changes onto the main branch. Most the time we would do a Pull Request but here is how to do it without doing that
Here we have two branches with two commits on the second branch

We can merge E and C to conceptually F with the merge command and the current branch with D and E commits is merged to the parent branch. When doing this you are prompted to add a message

git merge <branch>

We can see a rather dodgy graph with this

git log --oneline --decorate --graph --parents

In the picture below we can see commit e447ac9 and 7a088e8 are merged together

Fast Forward Merge

This is here in case I forget (as if). It we make a branch called new_feature and no changes have occurred on the main branch then you merge you new_feature branch as above, then this is a fast forward merge. So it is a term that refers to the mechanics of git.

Rebasing

Never done this. I have pulled my stuff from the main branch to keep up. So here is a picture of rebasing to help anne oldman (see what I did there) out. Before out feature branch was branched a A

We decide we want to move and rebase at C.