5. Git Hints

5.1. Download a Remote Branch to Replace a Local Branch

Here we are working with 2 upstream remotes

  1. my remote call it <origin>

  2. the project remote call it <upstream>

    git co dev git br -m dev_bckup #or git br <old_name> <new_name>

Find all remote branches and their upstream repositories, then fetch a specific remote branch from an upstream repository. Finally check out the upstream repository and create a local repository with that name. (Assumes you do not have a local repository named <dev>)

git br -r
# git fetch <remote_name> <branch_name>
git fetch upstream dev
# create a new branch dev from fetched branch
# git checkout -b <new_branch_name> <remote_name>/<remote_branch_name>
git co -b dev upstream/dev
# push dev to origin
# first delete origin/dev
git push origin --delete dev
# then push new dev to origin- git push <remote_name> <branch_name>
git push origin dev

5.2. Delete a Local and Remote Branch

git br -d <local branch name>
git push <remote name> --delete <remote branch name>

5.3. Create a New Branch from a Previous Branch

git co -b <new branch name> <previous branch>

5.4. Remove Cached Files from Git Tracking

git rm <filename>
git rm -r <directory>

5.5. Rebase

Git Rebase will take commits from your current working branch and apply them to the head of the base branch. Make sure master or base is latest from upstream/master and latest master is pushed up to origin

git co latest_feature_branch
# Rebase current master onto feature branch
git rebase master
# now latest_feature_branch can be merged with master

5.6. Remove Untracked Files

The command to remove untracked files is ‘git clean’. Before rmoving your untracked files use the -n option to tell you what will be removed. You may changed your mind and add them to the project or put the filename in your .gitignore file. ‘-d’ also removes untracked directories.

git clean -d n
# Output:
# Would remove <filename1>
# Would remove <filename2>

If you want to permanently remove an untracked file.

$ git clean -d -f
#Output
# Removing <filename1>
# etc. This deletes the files!

Interactive mode.

$ git clean -d -i

5.7. Rename a Branch

$ git co dev
$ git br -m dev_bckup
#or
$ git br <old_name> <new_name>

5.8. Use GitHub to Create a New Repository from the Command Line

Create local git repository. Install gh. Make sure you have your login credentials ready.

$ gh repo create <project_name>

Go through a series of choices and set the remote.

5.9. Use Git with Heroku CLI

https://devcenter.heroku.com/articles/git

Update Heroku, I believe it requires a restart after the update

heroku update

Create an app in Heroku

$ heroku create
Creating app... done, ⬢ sheltered-brook-17119
https://sheltered-brook-17119.herokuapp.com/ | https://git.heroku.com/sheltered-brook-17119.git

Add a remote to your local repository. Note that heroku only deploys code to the master or main branch

$ heroku git:remote -a sheltered-brook-17119
set git remote heroku to https://git.heroku.com/sheltered-brook-17119.git
# check the remote was created
$ git remote -v
heroku  https://git.heroku.com/sheltered-brook-17119.git (fetch)
heroku  https://git.heroku.com/sheltered-brook-17119.git (push)
# rename a remote if you wish
$ git remote rename heroku heroku-staging