#Git Cheatsheet
"...How many anosluke you need for feel the force..."
Create a new git repo in our working directory
$ git init
This command takes a remote name and a repository URL
$ git remote add origin https://github.com/try-git/try_git.git
What are the files changed in working directory?
$ git status
So we've made a few commits. Now let's browse them to see what we changed.
Fortunately for us, there's git log. Think of Git's log as a journal that remembers all the changes we've committed so far, in the order we committed them.
$ git log
History of changes for file with diffs
$ git log -p <file>
Take a look at what is different from our last commit by using the git diff command. In this case we want the diff of our most recent commit, which we can refer to using the HEAD pointer.
$ git diff HEAD
Complete conflicts diff between too commits with too ids
$ git diff <id-A> <id-B>
Complete conflicts diff
$ git diff
Against base file
$ git diff --base <file>
Against your changes
$ git diff --ours <file>
Against other changes
$ git diff --theirs <file>
If run git diff with the --staged option you can see the changes you just staged.
$ git diff --staged
To tell Git to start tracking changes made to <file.extension>, we first need to add it to the staging area by using git add.
$ git add .
$ git add <file.extension>
$ git add '*.txt'
You can unstage files by using the git reset command
$ git diff --staged
$ git rm <file.extension>
$ git rm '*.txt'
** importan! ** this command remove file to the working tree
We can check for changes on our Git repository and pull down any new changes. The -u tells Git to remember the parameters, so that next time we can simply run git pull and Git will know what to do.
$ git pull -u origin <branch>
When developers are working on a feature or bug they'll often create a copy (aka. branch) of their code they can make separate commits to. Then when they're done they can merge this branch back into their main master branch.
$ git branch <branche>
You can create and switch to new branche
$ git checkout -b <branche>
You can list the branch in your repository with.
$ git branch
$ git branch -av
When you have to merge your changes from the into the .
-
Go to
$ git checkout <branche-A>
-
Merge in my current branch
$ git git merge --no-ff <branche-B>
You can list the branch in your repository with.
$ git branch -d <branche>
important! this does not merge them
$ git fetch origin <branche>
Does fetch followed by a merge
$ git pull origin <branche>
To store our staged changes we run the commit command with a message describing what we've changed.
$ git commit -m "Commit example file to can push"
The push command tells Git where to put our commits when we're ready, and boy we're ready.
The name of our remote is origin and the default local branch name is master. The -u tells Git to remember the parameters, so that next time we can simply run git push and Git will know what to do.
$ git push -u origin <branch>
$ git tag v1.0
Files can be changed back to how they were at the last commit
- : name of file
- : id version of a file
Now the commands posibles are:
$ git checkout -- <file>
$ git checkout <id>
$ git checkout <id> <file>
$ git revert HEAD
** important!: you cannot undo a hard reset **
$ git reset --hard