scottfontenot
8/9/2017 - 12:30 AM

Git faq

Git faq

Fork 0 jpmariano/snippets

https://confluence.atlassian.com/display/STASH/Basic+Git+commands

git init - Create a new local repository

Remote Repositories

  1. Go into the folder that you want to push remotely
  2. git remote - to see if there's any remote repositories locally
  3. git remote add - to upload your files in github. Github should give you a detailed instructions git remote add origin https://github.com/... git remove -v - to see what url to fetch and pull git git push -u origin master - to push to master
  4. cat .git/config ls -la .git/refs/remotes/origin git branch -r - to see remote branch git branch -a - to see both remote and local branch

git clone /path/to/repository - Check out a local repository git clone username@host:/path/to/repository - Check out a remote repository

Tracking Remote Branch cat .git/config git config branch..remote origin git config branch..merge refs/heads/master git branch --set-upstream origin/

Pushing Changes to a Remote Repository

  1. Make sure all your commits are committed locally git log --oneline - to see local repo log git log --oneline origin/master - to see remote repo log git diff origin/master..master - to compare local branch to remote branch
  2. git push origin master

Fetch Changes from remote Repo git fetch origin - to get the changes from the remote repositories git merge git pull - git fetch + git merge git log --oneline -5 origin/master - to see the log of remote repo git log --oneline -5 master - to see the log of local repo

git branch origin/ - creates a tracting branch git branch --branch origin/ - creates a tracting branch and gets commited right away

git add - Add one or more files to staging (index):

git commit -m "Commit message" - Commit changes to head (but not yet to the remote repository): git commit -a - Commit any files you've added with git add, and also commit any files you've changed since then:

Note: Message should be lessthan 50 Char, complete description, add ticket number (optional), buttlet points (optional), short hand notation per company standard (Ie. js, css)

Head - last commit of what branch that you are on Cat HEAD- to see where the head Navigate to the Head and use Cat branchname

git log - to view lates commit, it contains commit id, author, date git help log git log -n 2 - returns 2 commit git log --since="2012-06-05" git log --until="2012-06-05" git log --author="john"

git push origin master - Send changes to the master branch of your remote repository:

git status- Status List the files you've changed and those you still need to add or commit:

git remote add origin - If you haven't connected your local repository to a remote server, add the server to be able to push to it: git remote -v - List all currently configured remote repositories:

Branches

git checkout -b - Create a new branch and switch to it:
git checkout - Switch from one branch to another:
git branch - List all the branches in your repo, and also tell you what branch you're currently in: git branch -d - Delete the feature branch: git push origin - Push the branch to your remote repository, so others can use it: git push --all origin Push all branches to your remote repository:
git push origin : - Delete a branch on your remote repository:

Update from the remote repository

git pull Fetch and merge changes on the remote server to your working directory:
git merge To merge a different branch into your active branch:

View all the merge conflicts:

View the conflicts against the base file:

Preview changes, before merging:

git diff git diff --staged git diff --base

git diff After you have manually resolved any conflicts, you mark the changed file:
git add Tags

You can use tagging to mark a significant changeset, such as a release: git tag 1.0.0 CommitId is the leading characters of the changeset ID, up to 10, but must be unique. Get the ID using: git log Push all tags to remote repository: git push --tags origin Undo local changes

If you mess up, you can replace the changes in your working tree with the last content in head:

Changes already added to the index, as well as new files, will be kept.

git checkout -- Instead, to drop all your local changes and commits, fetch the latest history from the server and point your local master branch at it, do this:
git fetch origin

git reset --hard origin/master Search

Search the working directory for foo(): git grep "foo()" Contact GitHub API Training Shop Blog About

VIM Environment i - to get into insert mode; Then write your message esc - to get back into normal mode :x or :wq - to quit and save and go back to git

git status - to check status and to know what branch you are in git add . - add everyfiles git add *.html - add only html files

git stash - when you are not ready to commit git stash apply - to continue editing your repositories

Deleting files

  • right click delete the file
  • git status
  • git rm
  • git status
  • git commit -m "Delete file"

or git rm - directly

Moving renaming file

  • right click change the filename
  • git add
  • git rm
  • git commit -m "rename file" or git mv git mv <newfoldernocation/newfilename>
  • git commit -m "moved the files"

Undoing Changes git checkout -- index.html - make the index.html same as from the repository git reset HEAD resources.html - reset the resources.html the same from the respository's HEAD git commit --amend -m "ammend message" - change the message of your last commit buut it creates a new commit git checkout -- resources.html - replaces current resources.html with the commit number that you indicated git revert - reverts to whatever commit you made git reset - specify where the head pointer git reset --soft - all it does is that it moves the head pointer git reset --mixed - (default), changes are not gone and ready to re-staged git reset --hard - move back to specific point in time, but it is still movaeable git reset HEAD - unstages changes git clean -n - tells the file to remove git clean -f - deletes untract files cat .git/HEAD

Ignoring files

  • create .gitignore file - using nano .gitignore inside .gitignore: - ignore file that you enter *.log - this ignores all the files with .log extension !index.php - track this file foldername/folder/ - ignore all files in that directory

Ignore files in all repositories or user specific git config --global core.excludesfile /.gitignore_global nano folderlocation/.gitignoreglobal cat <folderlocation/.gitconfig

Ignore track files add the files to the ignore git rm --cached - removed from staging git add .gitignore git commit - m "remove file"

Tracking Empty Directories touch <folderlocation/foldertokeep>/.gitkeep

Navigate repository commit tree

Commit number Head Branch Tag

  • Parent Commit -HEAD^, ^, ^ -HEAD~1, HEAD~ Grand Parent Commit -HEAD^^, ^^, ^^ -HEAD~2 Great Grand Parent Commit -HEAD^^^, ^^^, ^^^ -HEAD~3

git help ls-tree git ls-tree git ls-tree git ls-tree

git log --oneline - gives one list of your log git log --oneline -3 - shows last 3 commits git log --since="2012-06-20" git log --until="2012-06-20"

git log --since="2 weeks ago" --until="3 days ago" git log --since="2.weeks" --until="3.days git log --author="kevin" git log --grep="temp" git log <fromcommitnum..tocommitnum> --oneline git log <fromcommitnum..> - check what happen to that file in that particular commit git log -p - shows a diff of each changes git log -p git log --stat --summary git log --graph --oneline --all --decorate

viewing commits git show git show --format==oneline <commitnum/tree/blob> git diff --staged git diff --cached git diff git diff git diff <previouscommitnum..previouscommitnum> or <branchname..otherbranchname> or <branchname..otherbranchname^> git diff <previouscommitnum..previouscommitnum> git diff --stat --summary <previouscommitnum..nextcommitnum> git diff --ignore-space-change <previouscommitnum..nextcommitnum> git diff --color-words <previouscommitnum..nextcommitnum> git diff -b <previouscommitnum..nextcommitnum> git diff -w <previouscommitnum..nextcommitnum>

Branching cat .git/HEAD ls -la .git/refs/heads - list the branches cat .git/refs/heads/master git branch - to see what branch you are on git branch - to create a new branch git checkout - to switch to the branch you need to work on git checkout -b - created the branch and switch at the same time git log --graph --oneline --decorate --all - to see all branches commitnum git branch --merged - to see if your current branch is updated with the master git branch -m - to rename the branch, make sure you are outside the branch that you tried to rename git branch --delete - you can't delete the branch that you are on __git_ps1 - to get out cd.. echo $PS1

Merge git merge - make sure your are in the receiving branch git merge --no-ff git merge --ff-only git commit -am "Commit Message""

Abort the Merge git merge --abort

git status - to find which file has a conflict To fix the conflict

  1. Make sure you are in the branch you are trying to merge to
  2. Open the file and look at the conflict
  3. Edit the file and remove git embeded syntax
  4. Save and close the file
  5. git add
  6. git commit

To avoid Conflicts

  • keep line short
  • keep commits small and focused
  • merge often
  • track changes to master (means make sure your banch is nsync with the master)

Stash git status - Tells you when to use stash git stash save "your comment" - create a stash git log --oneline -3 git stash list - to see the list of stash, you can apply stash on any branch git stash show - to see the details git stash show -p - to see the details git stash pop - pulls the stash but leaves a copy in the stash git stash apply - added your stash into your branch git stash drop - delete the stash

Contact GitHub API Training Shop Blog About © 2017 GitHub, Inc. Terms Privacy Security Status Help