Giter Site home page Giter Site logo

git-cheatsheet's Introduction

#git Cheat Sheet

###always pull from the same git config branch.master.remote origin git config branch.master.merge refs/heads/master

###Start from existing code (-u is to always push to the same)

git remote add origin [email protected]:trufa/test.git
git push -u origin master 

###Use kdiff3 as mergetool to solve conflicts

(paste this inside of your .git/config file)

[merge]
    tool = kdiff3

[mergetool "kdiff3"]
    path = C:/Program Files/KDiff3/kdiff3.exe
    keepBackup = false
    trustExitCode = false

###Four ways of undoing last n commits

Read explanation here!

You want to nuke commit your and never see it again (Careful! Permanent loss).

git reset --hard HEAD~n

You want to undo the commit but keep your changes

git reset HEAD~n

Undo your commit but leave your files and your index:

git reset --soft HEAD~n

If you only want to change the last commit message

git commit --amend -m "New commit message"

###Retrieve a specific version of a file

git show <commit-id>:<path>

###Basic branching (tutorial)

Checkout to a new branch

git checkout -b newBranchName

Go back and merge

git checkout master
git merge newBranchName

###Help for resolve merge conflicts

When at a merge conflict, find changes between the common ancestor and the file on our branch which was merged

git diff :1:<path> :2:<path>

Find changes between the common ancestor and the file on the branch we are merging into the current branch

git diff :1:<path> :3:<path>

###Revert uncommited file to last version

git checkout filename

###Set vimdiff as default difftool (source)

git config --global diff.tool vimdiff
git config --global difftool.prompt false
git config --global alias.d difftool

###Stash local changes to be able to pull without errors (source)

git stash save --keep-index

###Remove untracked files

Files are lost permanently. Use -n or --dry-run to preview the effected files.

####Remove only untracked files

git clean -f

####Remove untracked files & directories

git clean -f -d

###Check if there is anything to pull, bring remote up to date (source)

git remote update

###Know what you pulled (source)

git diff master master@{1}

###Checkout to a remote branch

git checkout -b abranch origin/abranch

###Move uncommitted changes to a new branch (source)

git checkout -b <new-branch>
git add <files>
git commit

###Move uncommitted changes to existing branch (source)

git stash
git checkout branch2
git stash pop

###Get commits by certain user (source)

git log --author=<pattern>

###Rename a file

git mv dir/oldName dir/newName

And commit the changes.

Note: the file needs to be added (git add file).

###Exclude file from diff (source)

Create a repository specific diff driver with this command

git config diff.nodiff.command /bin/true

or for all your repos with --global.

Assign the nodiff driver to those files you want ignored in your .git/info/attributes file.

irrelevant.php    diff=nodiff

###See what was changed by one specific commit

git show HashOfTheCommmit

###See history of one file (source)

gitk fileName

Or

git log -p fileName

###List changes between two branches

List changes files

git diff --name-status firstBranch secondBranch

List changed files and number of changes

git diff --stat --color firstBranch secondBranch

List changed commits in one branch but not another (in secondBranch not in firstBranch).

git log --oneline ^firstBranch secondBranch

###Tags (annotated, the kind you can push to another repo)

Make an annotated tag

git tag -a -m "Tag for 1.2.3.4 release" release-1.2.3.4

Push an annotate tag to another repo

git push origin release-1.2.3.4

or push all tags

git push --tags origin

Rename a tag in local and remote repo

git tag newTagName oldTagName
git tag -d oldTagName
git push origin :refs/tags/oldTagName
git push --tags

###Repo splunking

List files in the repo on abranch (use 'HEAD' for current branch)

git ls-tree --full-tree -r abranch | awk '{print $4}'

git-cheatsheet's People

Contributors

e40 avatar josephlord avatar madc avatar tlazaro avatar trufa avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.