Last active
August 31, 2018 09:53
-
-
Save nihal-singh/416ab8fe4914bbbe747dbf3c1c6bb798 to your computer and use it in GitHub Desktop.
Learn git in minutes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
* git init -initialise a new git repository | |
[initially the new repository contains 0 commits] | |
[commit is a snapshot of git repository] | |
* git log - log of previous commits | |
['head' is the git name for current commit] | |
* git log -n 2 - shows only last 2 commits [n here means number] | |
* git log --graph --oneline master coins - [this will log the commits in one line (master and coins are the branch names) ] | |
* git staus - tells the file that have been changed since last commit. | |
* git add [file_name] - adds the file to the staging area | |
[staging area acts as an intermediate b/w working directory and the repository] | |
[staging area includes the file to be commited in the repositry] | |
* git commit - opens a editor to write the commit message. | |
* git diff - (without arguments) compares the file in working directory which are not moved to the staging area | |
* git diff --staged - compares staging area files with the repository. | |
|###########| |###########| |############| | |
| | | | | | | |
| working |--->| staging | ---->| repository | | |
| directory |add | area |commit| | | |
| | | | | | | |
|###########| |###########| |############| | |
* git reset -If you accidentally add a file to the staging area, you can remove it using git reset. For example, if you accidentally add lesson_2_reflections.txt, but don’t want it to be committed yet, run git reset lesson_2_reflections.txt and the file will be removed from the staging area, but it will still be in your working directory. | |
* git reset --hard - discards any changes either in working dir or staging area | |
GIT BRANCHES | |
------------ | |
* git branch - shows the current branches (* in front of branch name means it is currently checked out | |
so this will update when i make a change.) | |
* git branch [argument] - creates a branch with name [argument]. | |
* git checkout - Temporarily reset all files in a directory to their state at the time of a specific commit. | |
[It takes one argument - the commit ID to restore] | |
* git checkout [branch_name] - makes the branch_name as checkout branch. | |
[remote branch means someone else have created that branch.] | |
* git checkout -b new_branch_name - we can use this command instead of using the above two commands. | |
[ch2 - video 26 and 27 of git tutorial] | |
* git merge master coins - merges branch master and coins | |
* git show commit_id- shows the changes made to the commit compared to its parent commit | |
* git branch -d coins - deletes the branch 'coin'.(delets only the label and not the commits linked to it) | |
* git gc -git garbage collection - If a branch is deleted and leaves some commits unreachable from existing branches, those commits will continue to be accessible by commit id, until Git’s garbage collection runs. This will happen automatically from time to time, unless you actively turn it off. You can also run this process manually with git gc. | |
* git diff -u file1 file2 - find difference b/w two files (u - unified diff format- make more readable | file1 -original ,file2- new file) | |
* git log --stat | |
* git config --global color.ui auto (global is used to make changes in all git projects) | |
* git config --global push.default upstream | |
* git config --global merge.conflictstyle diff3 | |
* git config --list - shows the config file contents. | |
* Caching your GitHub password in Git - we dont need to enter password everytime we commit to github [https://help.github.com/articles/caching-your-github-password-in-git/] | |
##*###*###*###*####*####*####*####*####**####***FINISH***####**####*#####*#####*#####*#####*#####*#####*######*##### | |
how bash prompt works | |
------------------------ | |
https://www.cyberciti.biz/tips/howto-linux-unix-bash-shell-setup-prompt.html | |
---------------------------------------------------------------------------------------------------------- | |
GIT MERGE NOTE | |
----------------- | |
Checking out the coins branch | |
If you haven't already checked out the coins branch, you'll need to do so now with the command git checkout coins before you'll be able to refer to it. Once you've done that, decide whether you should keep it checked out or check out a different branch before completing the merge. | |
A note about git merge | |
git merge will also include the currently checked-out branch in the merged version. So if you have branch1 checked out, and you run git merge branch2 branch3, the merged version will combine branch1 as well as branch2 and branch3. That’s because the branch1 label will update after you make the merge commit, so it’s unlikely that you didn’t want the changes from branch1 included in the merge. For this reason, you should always checkout one of the two branches you’re planning on merging before doing the merge. Which one you should check out depends on which branch label you want to point to the new commit. | |
Since the checked-out branch is always included in the merge, you may have guessed that when you are merging two branches, you don't need to specify both of them as arguments to git merge on the command line. If you want to merge branch2 into branch1, you can simply git checkout branch1 and then type git merge branch2. The only reason to type git merge branch1 branch2 is if it helps you keep better mental track of which branches you are merging. | |
Also, since the two branches are merged, the order in which they are typed into the command line does not matter. The key is to remember that git merge always merges all the specified branches into the currently checked out branch, creating a new commit for that branch. | |
Merge conflict | |
If you get a message like this | |
*************************************************************** | |
Auto-merging game.js | |
CONFLICT (content): Merge conflict in game.js | |
Automatic merge failed; fix conflicts and then commit the result. | |
****************************************************************** | |
then your files were not in the same state as Caroline's when you started the merge. To fix this, complete the following steps: | |
Restore your files to their state before you started the merge by running git merge --abort | |
Double check the state of your files. If you run git log while the master branch is checked out, you should see Caroline's "Add color" commit as the second-most-recent, and the most recent should be your commit fixing the bullet bug. If you use git diff to compare your commit to Caroline's, your commit should introduce the line this.delayBeforeBullet = 10; on line 424. The line should be indented to the same level as the line below it using only spaces (no tabs), and the line should have no spaces after it. | |
Once your file is in the correct state, create a new commit with your changes. | |
Try the merge again. | |
Merge conflict (Newline characters between Windows and Unix systems) | |
---------------------------------------------------------------------- | |
Context: Whenever we hit the "Enter" key on the keyboard, we are actually telling the computer to insert an invisible character into our text file to indicate to the computer that there should be a new line. Unix systems adds one character called the "line feed" character or LF or \n while Windows systems adds two characters, "carriage return" and "line feed" or CRLF or \r\n. | |
Caroline's files have LF because her files were edited on Mac OSX, which uses LF. If a Windows user were to edit Caroline's files, the Windows text editor might convert all LF to CRLF to make editing files possible. When the Windows user merges her file with Caroline's files, a merge conflict will result due to the different LF and CRLF characters. | |
To fix this, Windows users should set the global autocrlf attribute to true: git config --global core.autocrlf true. More information can be found here: https://help.github.com/articles/dealing-with-line-endings/#platform-all | |
Comparing a commit to its parent | |
To compare a commit to its parent is git show commit_id | |
----------------------------------------------------------------------------------------------------------------------------- |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment