Skip to content

Git FAQ

Gerrie Bouwer edited this page May 18, 2021 · 1 revision

SVN to Git

Google doc with basic information found here

FAQ

Bash commands below can be used in Git Bash.

What is the typical workflow for a branch?

  1. Create a new branch and switch to it.
  2. Make changes to the code ... "Stage" changes and commit locally. (Repeat as necessary)
  3. Push changes to remote.
  4. Create a PR (pull request)

Using the same protocol as used for goodxweb; always amend previous commit. Thus , first commit will be git commit, any subsequent commit will be git commit --amend

# Create the branch (from main) and switch to it
# Necessary to first check out the LATEST code for "main"
$ git checkout main
$ git pull 
$ git checkout -b {branch_name} 

# make changes to code ...
# check what changed
$ git status 

# local commit of changes; 
# "add" what you want to commit.
$ git add src/sample.sql

# then use "commit"
# NB!! FIRST COMMIT, thus no "--amend"
# git will prompt you with standard commit dialog ($EDITOR)
$ git commit 

# make further changes to code ... and add and commit those
$ git add src/input.txt

# git will prompt you with standard commit dialog ($EDITOR)
$ git commit --amend

# Happy with changes, push it to remote
$ git push -u origin {branch_name}

# Make pull request (PR) on github.com
# If changes requested the following steps are followed...

# make changes to code ...
# check what changed
$ git status 

# local commit of changes; 
# "add" what you want to commit.
$ git add src/sample.sql

# git will prompt you with standard commit dialog ($EDITOR)
$ git commit --amend

# Push to remote
# Require additional argument --force-with-lease because you amend on a branch already on remote
$ git push origin {branch_name} --force-with-lease

ToDo: UI equivalents

How do I change the default editor used by git for commit messages?

Git bash uses an editor to prompt the user for commit messages, etc. You can customize this editor by adding an entry to the config. Config can be on system, global or local level.

For instance, to set a system wide config (i.e. not repo specific) to use Notepad++ as editor, run the following in your Git Bash console:

# write config
$ git config --system core.editor "\"C:\Program Files (x86)\Notepad++\notepad++.exe\" -multiInst -notabbar -nosession -noPlugin"

# check config 
$ git config --system core.editor
"C:\\Program Files (x86)\\Notepad++\\notepad++.exe" -multiInst -notabbar -nosession -noPlugin

How should I use my GIT token instead of my credentials?

Basic authentication using a password to Git is deprecated and will soon no longer work. Visit https://github.blog/2020-12-15-token-authentication-requirements-for-git-operations/ for more information around suggested workarounds and removal dates.

Need clarification.

How do I do a rebase on my branch?

To be completed

How do I handle a rebase on my branch, when a file with the same filename has already been checked in on master (not by me)?

Source: Bennie Swart, 2021-05-07

Long version:

1. git checkout my-branch 
2. git rebase master //  Conflict (eg. 1234.sql already exists on master and I did not realise it.. )
3. git rebase --abort // abort, so that you can start again
4. Figure out how to fix your branch   ( eg rename 1234.sql to  1236.sql )
   You can do this by:
      1. physically renaming the file 
      2. OR  git mv src/oldbackend/db/init/db_diff/1234.sql src/oldbackend/db/init/db_diff/1237.sql 
6. git commit (--amend  if necessary) // new commit, or amend existing one
7. git rebase master // easy

Advanced short version (if you know what you want to do):

1. git checkout my-branch 
2. git mv src/oldbackend/db/init/db_diff/{1234,1237}.sql     // This moves the file
3. git commit (--amend  if necessary) 
4. git rebase master 

3 How do I rename a local branch?

# If you are on the branch in question
git branch -m {new_name}

# If you are not on the branch in question
git branch -m {old_name} {new_name}

How do I rename a local AND remote branch?

From https://linuxize.com/post/how-to-rename-local-and-remote-git-branch/

# Start by switching to the local branch which you want to rename:
$ git checkout {old_name}

# Rename the local branch by typing:
$ git branch -m {new_name}

# At this point, you have renamed the local branch.
# If you’ve already pushed the {old_name} branch to the remote repository , perform the next steps to rename the remote branch.
# Push the {new_name} local branch and reset the upstream branch:
$ git push origin -u {new_name}

# Delete the {old_name} remote branch:
$ git push origin --delete {old_name}

That’s it. You have successfully renamed the local and remote Git branch

How do I change an earlier commit message after I pushed it to remote?

From https://www.educative.io/edpresso/how-to-change-a-git-commit-message-after-a-push

$ git commit --amend -m "New message"
$ git push origin {branch_name} --force-with-lease

How do I delete a local branch? (I.e. branch have not been pushed to remote yet)

NB: This will delete all local changes made on the branch!

$ git branch -d {branchname}

ToDo: UI equivalents

How do I delete a branch on local and remote?

NB: This will delete the branch locally with all local changes AS WELL AS remove the branch from the repo!! Only use if you are SURE what you are doing.

# delete local
$ git branch -d {branchname}
# delete remote
$ git push origin --delete {branchname}

How do I remove all local branches not on remote?

NB: CAN LEAD TO DELETION OF VALID CODE!! Only use if you're SURE that all local work has been pushed to remote.

From https://coderwall.com/p/up1qma/git-remove-local-branches-not-on-remote

$ git branch -r | awk '{print $1}' | egrep -v -f /dev/fd/0 <(git branch -vv | grep origin) | awk '{print $1}' | xargs git branch -d

How to squash (merge several commits into one):

In Git you can merge several commits into one - this is called squashing.

This website explans a basic squash very nicely. Useful to do before pull request, since the policy is, to always only add a single entry on the main branch. https://www.internalpointers.com/post/squash-commits-into-one-git Changing a commit message

During the squashing process a list of all the comments in the list of commits is displayed. The user is required to type in the required new comment, and delete the rest. If the comment is not what was planned you can change it: https://docs.github.com/en/github/committing-changes-to-your-project/changing-a-commit-message