Skip to content

Instantly share code, notes, and snippets.

@bzon
Last active July 19, 2023 22:28
Show Gist options
  • Save bzon/81ba2a03ebfd76656af563e5ca41243b to your computer and use it in GitHub Desktop.
Save bzon/81ba2a03ebfd76656af563e5ca41243b to your computer and use it in GitHub Desktop.
GIT_HACKS

Common Git hacks

Rebasing develop branch from the latest changes of master branch.

git clone http://projectA.git -b develop
git pull --rebase origin master
# Resolve merge conflicts
git add <the resolved files>
git rebase --continue
git commit -m "Merged from master branch" # or git commit --amend -m "Overwriting the current commit"
git push origin develop

Merge develop branch changes to master branch.

git clone http://projectA.git -b master
git merge develop
git commit -m "Merge from develop branch"
git push origin master

Gerrit hacks

Save this as .gitconfig under your HOME directory.

[user]
    name = John Bryan Sazon
    email = john.bryan.j.sazon@accenture.com
[alias]
    up = !sh -c 'git push origin HEAD:refs/for/$1' -
    sandbox = !sh -c 'git push -u origin HEAD:sandbox/$1' -
    fsandbox = !sh -c 'git push -f -u origin HEAD:sandbox/$1' -
    r = !sh -c 'git rebase -i origin/$1' -
    review = !sh -c 'refname=`printf refs/changes/%02d/$1/$2 $(($1 % 100))` && git fetch origin $refname && git checkout -b $1_$2 FETCH_HEAD' -
    unpushed = log --branches --not --remotes --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --date=relative
    gl = log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --date=relative
    dt = !sh -c 'git diff-tree --no-commit-id --name-only -r $1' -
    s = status
    b = branch
    co = checkout
    squash-for-review='git fetch && git reset --mixed origin/master && git merge --squash HEAD@{1}' -
    autocrlf = false
    editor = vim
[core]
    longpaths = true
    editor = vim
[credential]
    store = true

Using the .gitconfig hack above, commit and create a new Change and submit the first patch set.

git commit -m "I did my best" # Enter commit message
git up master # Creates a new change with patchset #1

^ That is equal to doing the following.

git commit -m "I did my best"
git push origin HEAD:refs/for/master

If review is not happy with in your changeset, update it by creating a new patch set.

# << Make changes  >> #
git commit --amend # Amend your commit, change the commit message if required, then save (command: !wq) the VIM terminal
git up master # Push the change, creating a new patch in the process

Rebasing a patchset from the origin branch, in this example from master branch.

git pull --rebase origin master
# Resolve merge conflicts
git add <the resolved files>
git rebase --continue
# Ensure you use --amend to ensure updating the same changeset and avoid creating a new changeset.
or git commit --amend -m "Your commit message" 
git up master

Easy way of force merging a change/patchset using the .gitconfig

git pull <scheme>://<project> -b master # Clone the master project
cd <project> 
git review <change_number> <patchset_number> # Fetch and pull the code
git checkout master # Switch to master
git merge <change_number>_<patchset_number> # Merge the change locally
git push origin master # Push changes

Common way of force merging a change/patchset.

# Clone the main project parent branch
git clone http://john@gerrithost.com/a/ProjectA.git
# Fetch the patch set
git fetch http://john@gerrithost.com/a/ProjectA.git refs/changes/15/2215/2 && git checkout FETCH_HEAD
# Check out master
git checkout master
# Merge the patch set virtual branch
git merge 0c7db41
# Push the change
git push
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment