Git Commands
Gitflow: https://www.atlassian.com/git/tutorials/comparing-workflows/gitflow-workflow
https://rehansaeed.com/gitattributes-best-practices/ | gitattributes-best-practices
https://www.educative.io/edpresso/the-fatal-refusing-to-merge-unrelated-histories-git-error | refusing-to-merge-unrelated-histories
https://medium.com/@bilalbayasut/github-how-to-make-a-fork-of-public-repository-private-6ee8cacaf9d3 | how-to-make-a-fork-of-public-repository-private
https://superuser.com/questions/232373/how-to-tell-git-which-private-key-to-use
git defaults / globals
You can use git config –list
, or look at your ~/.gitconfig
file. Local config will be in your repository's .git/config
file.
When initiating git on a computer:
git config --global push.default matching
git config --global user.email "jim.obrien3@gmail.com"
git config --global user.name "Jim O'Brien"
git config --global credential.helper cache
git config --global credential.helper 'cache --timeout=3600'
git config --global core.autocrlf input
git config --global core.eol lf
git config --global core.longpaths true
Rename Limit:
git config merge.renameLimit 999999
git config --unset merge.renameLimit
- to unset
Setting up IDEs
git config --global core.editor "atom --wait"
- atom
git config --global core.editor "subl -n -w"
- sublime
git config --global core.editor "mate -w"
- textmate (for mac)
git config --global core.editor "code --wait"
- visualstudio code
Case Insensitivity
git config core.ignorecase false
# Configure Git on Linux to properly handle line endings
Quick start
git pull --rebase
git add .
git commit -m "commit message"
git push
git stash
- stash your changes
git stash apply
-apply your changes
Let's get started
git init
- Create a new local repository
git help <command>
- Get help with a specific git command.
Checking what's up
git status
- Lists every file that has changed since the last commit, and files currently staged for commit
git diff
- Lists every individual change to every file that has changed since the last commit. Add the '–staged' tag to show changes currently staged for committing.
Committing
git add .
- Add all current changes to the staging area, ready for the next commit.
git add -p <filename>
- Add some changes in <filename>
to the staging area, ready for the next commit.
git commit -A
- Commit all local changes (additions and removals) in tracked files to the staging area, ready for the next commit.
git commit
- Commit previously staged changes.
git commit <filename> -m 'My commit message'
- Commits changes to the <filename>
file.
The-m
flag expects to be followed by a commit message surrounded by quotes. Single quotes if you want to use quotation marks in your message, or double quotes if you want to use apostrophes in your message. If you want to commit everything in a sub directory,git commit sub-dir -m 'message
'. If you want to commit everything in the repo without staging it first (dangerous),git commit -a -m 'message
'.
git commit --amend
- Change the last commit. Don‘t amend published commits!!
$ git config credential.helper store $ git push https://github.com/repo.git Username for 'https://github.com': <USERNAME> Password for 'https://USERNAME@github.com': <PASSWORD>
- save your username and password for pulls and pushes
git config --global credential.https://github.com.username <your_username> git config --global --edit
- this may be useful
git help credentials
- for more info
git config --global user.name "YOUR NAME" git config --global user.email "YOUR EMAIL ADDRESS"
- use this to set your name and email address for commits
git remote add origin https://github.com/username/repository.git
- if you have an existing repo this is how you can add the remote url so you can commit your code to the repo
git remote set-url origin git@github.username/repository.git
- if you have a broken link form the local to the remote use this command to fix it
git remote show origin
- use this to see what remote set-url is currently
git config --get remote.origin.url
- use this if referential integrity has been broken
git commit --no-verify -m "message for commit" git commit -mn "message for commit"
- This will skip and precommit checks for getting past phpcs, phpcbf, or jslint
Commit History
git log --oneline
- Show all commits, starting with newest. The oneline flag cleans the entries up a bit to display each commit on one line.
git log --oneline --graph --decorate --all
- use this to see a detailed tree with colors of the branch flow
git log -p <file-name>
- Show changes over time for a specific file.
git blame <file-name>
- Display who changed what in <file-name>
.
git blame -L 10,20 <file-name>
- Will display who edited those specific lines of <file-name>
.
Branching Out
git checkout <branch-name>
- Allows you to switch between git branches, adding the -b
flag creates the branch you are switching to if it doesn't already exist. You can also specify specific commit hashes instead of branch names.
If you want to checkout a specific file you can git checkout <branch-name-or-commit> <file-path>
which is powerful as it allows you to selectively merge in files or rewind changes.
git branch
without any parameters will list all current branches. You can also use it to create a new branch with
git branch <new-branch-name>
which will create a branch based on your current HEAD but not check it out (which is why git checkout -b <new-branch-name>
is so useful).
git checkout --track <remote-name>/<branch-name>
- Create a new tracking branch based on a remote branch.
git branch -d <branch-name>
- Delete a local branch.
git push origin --delete <branch-name>
- Delete a remote branch.
git tag <tag-name>
- Mark the current commit with a tag.
git branch cleaning
git fetch -p && for branch in `git branch -vv | grep ': gone]' | awk '{print $1}'`; do git branch -D $branch; done
- remove local branches that have been removed in remote already
https://stackoverflow.com/questions/7726949/remove-local-branches-no-longer-on-remote
Once, I was trying to delete old branches on my local machine. I had already deleted a bunch of branches on github using the UI interface. Here is how I deleted my local branches after deleting them on remote:
git fetch -p
- removes remote references
git branch -vv
- will show “gone” as the remote status
git fetch -p && for branch in `git branch -vv | grep ': gone]' | gawk '{print $1}'`; do git branch -D $branch; done
- simple script to remove local branches that have gone remotes
git line ending problems
https://gist.github.com/shiftkey/b51f29301e52a3bc74d9
https://help.github.com/articles/dealing-with-line-endings/
If you get issues with line endings make sure you are running a newer version of git, and if you still have problems look at this link: https://stackoverflow.com/questions/5834014/lf-will-be-replaced-by-crlf-in-git-what-is-that-and-is-it-important
Phoning Home
As evidenced by the popularity of remote repo hosting services (hello Github!), One of the most powerful features with git is the ability to track remote repositories.
git remote -v
- List all currently configured remotes. The -v
flag stands for verbose and allows you to see more information about the remote repos, like the url.
git remote show <remote-name>
- Show more information about a specific remote.
git remote add <remote> <url>
- Add new remote repository, named <remote>
and with a source url of <url>
.
git fetch <remote>
- Download all changes from <remote>
, but don‘t integrate into HEAD.
git fetch --dry-run -v
- –dry-run
Show what would be done, without making any changes. -v
Be verbose.
git pull <remote> <branch-name>
- Download changes and directly merge/integrate.
git push <remote> <branch-name>
- Publish local changes to the remote repo.
git branch -dr <remote-name>/<branch-name>
- Delete a branch on the remote repo.
git push --tags
- Publish any tags you've added.
Making Synergy!
Dealing with differences and changes between repos is the real power behind git.
git merge <branch-name>
- Merge <branch-name>
into your current HEAD.
git rebase <branch-name>
- Rebase your current HEAD onto <branch-name>
. Don‘t rebase published commits!!
git rebase --abort
- Aborts a rebase in progress.
git rebase --continue
- A rebase will pause when it detects a conflict and will wait for you to resolve it/them by choosing which side of the merge to keep. When you've resolved the conflicts, this command continues the rebase.
git mergetool
- Use your configured merge tool to solve conflicts.
git add <resolved-file>
- Use your editor to manually solve conflicts and (after resolving) mark file as resolved.
git rm <resolved-file>
git merge --abort
- VERY HELPFUL if you try to merge master into your branch and have merge conflicts, and need to undo.
Ctrl/Cmd-Z!
We all make mistakes.
git reset --hard HEAD
- Discard all local changes in your working directory.
git checkout HEAD <file-name>
- Discard local changes in a specific file.
git revert <commit>
- Revert a commit (by producing a new commit with contrary changes).
git reset --hard <commit>
- Reset your HEAD pointer to a previous commit and discard all changes since then.
git reset <commit>
- Reset your HEAD pointer to a previous commit and preserve all changes as unstaged changes.
git reset --keep <commit>
- Reset your HEAD pointer to a previous commit and preserve uncommitted local changes.
git revert {commit-hash#}
- Use git log
to see commit hash and find the one you want to remove. Then, use this command to remove the last commit.
Tips and Tricks
git checkout -- `git ls-files -m`
- Reset modified files in a directory
rm -rf `git ls-files --other --exclude-standard`
- Remove untracked files (use caution)
git log -G "<search-string>" --pretty=format:"%C(yellow)%h %Creset%s %Cgreen(%cr) %Cblue[%cn - %ce]" --decorate
- Search commits for a string or code
The following snippet is handy if you're dealing with a monolithic repo with many branches and submodules:
git checkout <branch-name> && rm -rf `git ls-files --other --exclude-standard` && git submodule update --init --recursive
- Reset-checkout.. checks out branch, removes untracked files, and re-inits submodules
Set default message editor to VIM:
git config --global core.editor "vim"
git cheat sheet
No more GUI! Git CLI FTW! Much credit for this file goes to the incredible resource provided by Tower.
git remote add cdcgitlab git@git.cdc.gov:Templates/templatepackage.git git config core.sshCommand "ssh -i C:/Users/csrsdevjenkinssvc/.ssh/id_ed25519 -F /dev/null" git push cdcgitlab master
- how I got it to work on Jenkins server