Ubuntu: GIT

From The Travis van der Font Wiki
Jump to: navigation, search
Pulling a new project
git clone <git-https-url-address>
Updating to the latest files
git pull
Removing a project (directory)
rm -rf <repository-name>
Update the master branch of the cloned repository
git fetch origin          # fetch updates from origin remote
git merge origin/master
Update the master branch of the cloned repository (combine commands)
git pull origin master

Delete local master branch (must not be on master)

git branch -D master

Delete a local / remote branch

git branch -d <branch-name>             # local branch
git push origin --delete <branch-name>  # remote branch

Rename a local / remote branch

git branch -m <new-name>                # switch to the branch and rename it
git push origin :<old-name> <new-name>  # delete the old-name remote branch + push the new-name local branch
git push origin -u <new-name>           # reset the upstream branch for the new-name local branch

Temporarily switch to a different commit

# This will detach your HEAD, that is, leave you with no branch checked out:
git checkout 0d1d7fc32
# to allow commits from the checkout
git checkout -b old-state 0d1d7fc32
Hard delete unpublished commits (delete everything after the commit)
git reset --hard 0d1d7fc32
# keep all the commit changes but delete the commits
git stash
git reset --hard 0d1d7fc32
git stash pop
To change the previous commit message:
git commit --amend
git push --force <example-branch>
To revert to a previous commit, ignoring any changes:
git reset --hard HEAD
To revert to a previous commit with all changes available:
git reset --soft 56e05fced
To revert to a commit that's older than the most recent commit:
git reset 56e05fced
Rebase (flat merge) current branch into master:
git rebase -i origin/master  #compress commits
git rebase origin/master     #stack commits
Additional References:

Copying a version of a single file from one git branch to another

Run this from the branch where you want the file to end up:

git checkout otherbranch myfile.txt

General formulas:

git checkout <commit_hash> <relative_path_to_file_or_dir>
git checkout <remote_name>/<branch_name> <file_or_dir>

Some notes:

  • Using the commit hash you can pull files from any commit
  • This works for files and directories
  • overwrites the file myfile.txt and mydir
  • Wildcards don't work, but relative paths do
  • Multiple paths can be specified

an alternative:

git show commit_id:path/to/file > path/to/file

To save changes inside VIM (Console)

Hold [Escape] and press shift + ZZ (avoid console usage on JetBrains Terminal)

Remove all history from a repository
# clone the repo (skip if you already have a cloned repo locally)
git clone [email protected]:USERNAME/REPOSITORY.git
cd REPOSITORY

# remove all history locally
rm -rf .git

# create a new local repo
git init

# add everything
git add .
git commit -m "First commit"

# nuke history on GitHub (irreversible)
git remote add origin [email protected]:USERNAME/REPOSITORY.git
git push -u --force origin master

Reference(s)