Skip to content

Instantly share code, notes, and snippets.

@ljaraque
Last active November 2, 2020 00:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ljaraque/b5437dc8ee8d430046425e126a32da30 to your computer and use it in GitHub Desktop.
Save ljaraque/b5437dc8ee8d430046425e126a32da30 to your computer and use it in GitHub Desktop.
Git Quick Guide

**Git Quick Start Guide **

title: Git Quick Start Guide
date: 2017-01-30
by: ljaraque@yahoo.com

  1. Check current status of git repo: git status

  2. Create new branch: git checkout -b branch_name

  3. Switch between branches: git checkout branch_name

  4. If uncommited changes in current branch and want to switch to another for a while: git stash, then switch branch. When coming back, after git checkout branch_name, do git stash pop and uncommited changes will be recovered.

  5. Know current remote origin: git remote -v

  6. Change Remote Origin: git remote set-url origin https://bitbucket.org/user/repo_name.git

  7. Rename remote: git remote rename old_name new_name

  8. How to checkout a remote branch named remote/origin/name. Do git checkout name without remote/origin/

  9. Add additional remote to have multiple ones: git remote add additional_name url

  10. Multiple Remotes: use fetch instead of pull to avoid merging automatically. pull is fetch + merge. So uses git fetch remote_name. Directive --all will not work.

  11. How to get a local copy of a remote branch:

git checkout -b <branch_name> origin/<branch_name>

  1. How to transfer all branches from remote repository to local machine:
    Use the following script:
#!/bin/bash
for branch in `git branch -a | grep remotes | grep -v HEAD | grep -v master `; do
   git branch --track ${branch#remotes/origin/} $branch
done

then do:

git fetch --all
git pull --all

A complete local copy of the remote repo will be available in your local machine. This is useful, for example, when migrating a repository from one remote system to another.

  1. Removal of files previously tracked. This will remove from repo and from local directory in use:

git rm filename

  1. Removal of files previously tracked but keeping them in the directory in use:

git rm --cached filename

In Reference: http://source.kohlerville.com/2009/02/untrack-files-in-git/ it is said that in collaborative environments it is better to use:

git update-index --assume-unchanged [path]

(See point 19 below)

  1. Discard all changes and go back to last commit. If changes made on a branch are needed to be discarded, we have to reset the changes and then clean untracked, doing:

git reset HEAD --hard
git clean -fd

  1. TAGS: Specific commits can be tagged in order to deal with a more human readable name than hash codes.
  • Tag current commit (Short Version): If placed on a given commit then do git tag <tag_name>
  • Tag current commit (Long Version): If more description is needed then the tag can be added with git tag -a <tag_name>. An editor will be opened in order to input the description of the tag.
  • Check list of existing tags: git tag
  • Check which commit is associated to a specific tag: git show <tag_name> | head
  • Useful command to see all commits log in pretty way: git log --pretty=oneline | head. This will show the last 10 commits.
  • Delete a specific tag: git tag -d <tag_name>
  1. Git & Github: Assuming that you have forked repository from an original project https://github.com/user/repo_name.git into yout github account at https://github.com/your_user/repo_name.git. This repo will be up to date with the original one at the moment of the fork. In order to update the forked repo you have yo do:
git remote add upstream https://github.com/user/repo_name.git
git fetch upstream
git checkout <branch_name>
git rebase upstream/<branch_name>
git push -f origin <branch_name>

push -f is needed only the first time after rebase.

  1. Checking branch commits graph:

git log --graph --decorate --oneline --all

  1. Avoid update of files but keep them in repo (Not ignoring them): This is useful for files with dynamic data, where we need them in the initial repo clone, but then each implementation will overwrite with dynamic data meaningless for the repo.
  • Configure the file to not be tracked:

git update-index --assume-unchanged FILE_NAME

  • Configure the file to be tracked again:

git update-index --no-assume-unchanged FILE_NAME

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment