Git advanced notes

Archived

This page has been archived and will receive no further updates.

Branches

Rename a branch

  1. Rename the local branch
     git branch -m oldname newname
    
  2. Push the renamed branch
     git push origin newname
    
  3. Delete the remote branch
     git push origin --delete oldname
    

Create a new, empty branch

git checkout --orphan BRANCHNAME
git rm -rf .

Rewriting history

Delete pushed commits

Never do this if other people are using the repo!

If you wish to delete all commits after a certain one:

  1. Get the hash of the last commit you want to keep:
     git log -p
    
  2. Do a hard reset to that commit
     git reset --hard 2221651
    
  3. Do a force push to delete the remote commits Always specify the branch if you have more than one branch!
     git push --force
    

Delete a file completely from git history

https://help.github.com/articles/remove-sensitive-data/#using-filter-branch

cd /path/to/repo
git filter-branch --force --index-filter \
'git rm --cached --ignore-unmatch PATH-TO-YOUR-FILE-WITH-SENSITIVE-DATA' \
--prune-empty --tag-name-filter cat -- --all
git push origin --force --all
git push origin --force --tags

Merging

Merge specific files from one branch to another, without history

http://stackoverflow.com/a/1355990/399105

Workflows

Sample feature branch workflow

These instructions are based on a feature branch workflow: https://www.atlassian.com/git/tutorials/comparing-workflows/feature-branch-workflow

Rebasing instead of merging is used on the feature branch for cleaner project history: https://www.atlassian.com/git/tutorials/merging-vs-rebasing

  1. Pull the latest updates to the repo
     cd /path/to/repo
     git pull
    
  2. Create a new branch named after the feature
     git checkout -b FEATURE-NAME
    

    Ex:

     git checkout -b feature-add-logout
    
  3. Do work in the new branch, committing as needed
     git add /path/to/file
     git commit
    
  4. Push work periodically to the repo
     git push -u origin FEATURE-NAME
    

    Ex:

     git push -u origin feature-add-logout
    

    (The first push will start tracking the feature branch. After than, you can just use git push)

  5. Discuss changes with other users of the repository

  6. Prepare feature branch for merging
     git checkout master
     git pull
     git checkout FEATURE-NAME
     git pull
     git rebase master
    
  7. Merge feature branch
    • To merge your branch and keep all commits:
        git checkout master
        git merge FEATURE-NAME
        git push
      
    • To add work to the main branch as one commit (losing all of the individual commit logs):
        git checkout master
        git merge --squash FEATURE-NAME
        git commit -m "Commit message for your feature"
        git push
      
  8. If you’re finished with the feature branch, you can delete it
    1. Delete the branch locally
       git branch -d FEATURE-NAME
      
    2. Delete the branch remotely
       git push origin --delete FEATURE-NAME
      

Ignoring

Ignore local changes to a file in the repo

https://stackoverflow.com/a/20241145/399105