Dealing with Git
Here are some references and thoughts when dealing with git.
Basic Config
Type these for first time configuration only.
git config --global user.name "your_name"
git config --global user.email "your_email"
git config --global color.ui true
Here is a sample global git configuration:
[user]
name = Name
email = Email
[color]
ui = true
[github]
user = Username
If you want to set special user name for a repo just delete –global
Basic Usage
These are the steps for using git with github or without it in an easy way:
Clone
git clone https://github.com/username/repo.git
//or
git clone [email protected]:username/repo.git
Push
git push origin master
If you don’t want to be asked about your username and password for your Github account eveytime you push. Then you shoud
- Add new deploy key in your repository on github with your ssh public key
- if you clone it with [email protected]:username/repo.git you don’t need to type any thing else. if not then type this:
git remote set-url origin
[email protected]:username/repo.git
Pull
git fetch origin
git merge origin/master
If you have forked your repository and wanted to keep tracking with the creator of the repository:
git remote add --track master owner https://github.com/username/repo.git
git fetch owner
git merge owner/master
–track master: just track the master
Remotes
check available remotes:
git remote -v
Add a remote:
git remote add origin https://example.com/repo.git
Branches
These are the steps for creating branches, and then merging them
New branch
git checkout -b hotfix
or:
git branch hotfix
git checkout hotfix
Commit changes
git add .
git commit -m “msg”
Merge changes to master
git checkout master
git merge hotfix
Delete the branch (Optional)
git branch -d hotfix
Ignoring files
This is how to ignore files before adding them to the repository. Create this file in the root folder in the repository
vi .gitignore
Type your files and folders
Remove files
These are the ways of removing files from git repository in multiple situation
Keep them in the real drive
If you added files to a repository and after that you want to ignore them first type them in .gitignore, then type:
git rm -r --cached .
git add .
git commit -m ".gitignore is now working"
For specific file replace dot in the first command with file_name
Remove them from repository and real drive:
git rm file_name
Remove files from repository that are deleted from real drive
git status -s | grep " D" | awk '{print $2}' | xargs git rm
Advance Usage:
# Logs
git log --graph
git log --graph --oneline
git log --graph --oneline --decorate --all
Squash Commits to a Single Commit
To squash four commits into one, do the following:
$ git rebase -i HEAD~4
First one make it pick and the others make them squash. Then quit and save.To change the message of a commit before pushing the changes:
$ git commit --amend
if you have already pushed your changes to the git server and you want to push them again with the squash commits use –force. Example: git push --force origin master
Get changes from master into a branch
use rebase instead of merge to keep the history clean and avoid an extra commit of the merging
git checkout branch_name
git fetch
git rebase origin/master
Autocomplete:
First Get this file, and Then:
- Copy this file to somewhere (e.g. ~/.git-completion.bash).
- Add the following line to your .bashrc/.zshrc: source ~/.git-completion.bash
Remove all your local git branches but keep master
git branch | grep -v "master" | xargs git branch -D
Provide better commit messages:
$ vi ~/.git_commit_msg.txt
# If applied, this commit will...
# Explain why this change is being made
# Provide links to any relevant tickets, articles or other resources
$ git config --global commit.template ~/.git_commit_msg.txt
log history
git log --pretty=short -u -L 8,9:path_to_file
Remove Branch from original source
git push origin --delete BRANCH_NAME
References: