title: Git Quick Start Guide
date: 2017-01-30
by: [email protected]
-
Check current status of git repo:
git status
-
Create new branch:
git checkout -b branch_name
-
Switch between branches:
git checkout branch_name
-
If uncommited changes in current branch and want to switch to another for a while:
git stash
, then switch branch. When coming back, aftergit checkout branch_name
, dogit stash pop
and uncommited changes will be recovered. -
Know current remote origin:
git remote -v
-
Change Remote Origin:
git remote set-url origin https://bitbucket.org/user/repo_name.git
-
Rename remote:
git remote rename old_name new_name
-
How to checkout a remote branch named
remote/origin/name
. Dogit checkout name without remote/origin/
-
Add additional remote to have multiple ones:
git remote add additional_name url
-
Multiple Remotes: use
fetch
instead ofpull
to avoid merging automatically.pull
isfetch + merge
. So usesgit fetch remote_name
. Directive--all
will not work. -
How to get a local copy of a remote branch:
git checkout -b <branch_name> origin/<branch_name>
- 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.
- Removal of files previously tracked. This will remove from repo and from local directory in use:
git rm filename
- 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)
- 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
- 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>
- 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.
- Checking branch commits graph:
git log --graph --decorate --oneline --all
- 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