Git Product home page Git Product logo

git-tutorial's Introduction

Table of Contents:

Git Basics

3 local stage + 1 remote:

  1. Working directory
    • may or may not be managed by git, but git is aware of them
  2. Staging area - pre-commit holding area
    • git index: a holding area for queueing up changes for the next commit
  3. Commit - git repository (history in .git folder)
  4. Remote repository
Command Description
git status changes between: |working directory|staging area| repository (.git folder)|Remote

The git repository:

  1. Working directory (~/projects/my-project)
  2. Repository (/.git --> hidden)

Table of Contents

General

configurations first:

git config --global user.name "username"
git config --global user.email "email"
Command Description
git remote add origin <url> Adding remote to the repository
git remote -v Shows the list of remotes
git log
git alias
git init 'foldername to be created' shorten commands
ls -al see all folders including those starting with . (e.g: .git folder)
Command Description
git commit -am "message" add and commit
git commit --amend -m <message> fix the last commit
git ls-files list of tracked files

Note: git commit --amend will update and replace the most recent commit with a new commit that combines any staged changes with the contents of the previous commit. With nothing currently staged, this just rewrites the previous commit message.

Table of Contents

Git Visualizer

Click Git Visualizer to see visualization of git basic commands.

Undo Changes

Command Description
git reset HEAD <file> unstage the file
git checkout -- <file> back to the last commit

Note: HEAD is just a pointer.

Table of Contents

Renaming and Moving

Command Description
git mv <file> <newfile> one renaming operation
mv <file> <newfile> two operations: deletion and creation
git add -A recursively adds files, but also updates any files renamed, moved, or deleted

Table of Contents

History

Command Description
git help log
git log
git log --since "2 days ago"
git log --oneline show commits in one line
git log --oneline --graph --decorate show commits graph with branches (decorate)
git log --abbrev make commit ids abbreviated
git log -- <file> logs for a specific file
git show <commit_id> history of a specific commit

Table of Contents

Alias

Command Description
git config --global alias.<alias_name> <git Command> alias for a long Command (storead in ~/.gitconfig file which can be modified)

Examples:

git config --global alias.unstage 'reset HEAD --'
git config --global alias.hist 'log --all --oneline --graph --decorate'

Table of Contents

Ignoring unwanted files and folders

Add unwanted files and folders name to .gitignore file.

Example Description
myfile.txt specific file
*.ext file pattern
my-folder/ folder
  • Note: adding README.md does not work. git does not ignore readme files even if you add it to .gitignore.

Table of Contents

Comparison

  • Note: All comparisons are done for files tracked by git. If file is not in the list of git tracked files (git ls-files), the changes will not be shown.

Table of Contents

Setting vscode as difftool and mergetool

The way to wire them together is to modify your .gitconfig and you have two options.

  1. To do this with command line entries, enter each of these:
git config --global merge.tool vscode
git config --global mergetool.vscode.cmd "code --wait $MERGED"
git config --global diff.tool vscode
git config --global difftool.vscode.cmd "code --wait --diff $LOCAL $REMOTE"
  1. To do this by pasting some line in the .gitconfig with VS Code.

    • Run git config --global core.editor "code --wait" from the command line.
    • From here you can enter the command git config --global -e. You will want to paste in the code in the "Extra Block" below.
[user]
    name = EricDJohnson
    email = [email protected]

# Comment: You just added this via 'git config --global core.editor "code --wait"'
[code]
    editor = code --wait

# Comment: Start of "Extra Block"
# Comment: This is to unlock VSCode as your git diff and git merge tool    
[merge]
    tool = vscode
[mergetool "vscode"]
    cmd = code --wait $MERGED
[diff]
    tool = vscode
[difftool "vscode"]
    cmd = code --wait --diff $LOCAL $REMOTE
# Comment: End of "Extra Block"

Now in your git directory with a conflict, run git mergetool and you have VSCode helping you handle the merge conflict! To compare, run git difftool.

Turning of the Default Prompt

Command Description
git config --global diftool.prompt false By this setting, we will not have to deal with the prompt anymore.
git config --global mergetool.prompt false
git config --global diftool.prompt false git will not keep .orig files after merge

Note: After performing a merge, the original file with conflict markers can be saved as a file with a .orig extension.If this variable is set to false then this file is not preserved. Defaults to true (i.e. keep the backup files).

Table of Contents

Working Directory | Staged Area

Command Description
git diff all files changes
git diff <file> specific file changes

Table of Contents

Working Directory | Git Repository

Command Description
git diff HEAD changes for all files tracked by git with the last commit
git diff HEAD -- <file> changes for <file> with the last commit

Table of Contents

Staged Area | Git Repository

Command Description
git diff --staged HEAD changes for all files tracked by git comparison with the last commit
git diff --staged HEAD -- <file> changes for <file> with the last commit

Table of Contents

Commit | Last Commit

Command Description
git diff <commit1> <commit2> comparison between commit1 and commit2
git diff HEAD <commit1> changed compared to the last commit
git diff HEAD HEAD^ changed compared between last commit (HEAD) and the one before it (HEAD^)

Table of Contents

Local | Remote

Command Description
git diff master origin/master comparison between local repo (master) and remote repo (origin/master)

Table of Contents

Branching and Merging

Command Description
git branch list of all branches
git branch -a list of all branches including remotes
git branch <branch> making new branch (<branch>)
git branch -m <branch> <new_branch> renaming branch to new_branch
git checkout <branch> switch to branch <branch>
git branch -d <branch> removing local branch
git push origin --delete <branch> removing remote branch
Command Description
git checkout -b <branch> make new branch and switch to it
git diff master <branch> comparison between two branch
git merge <branch> merge branch <branch> with the current branch

Note: Fast-forwarded merge means git places all commits on the master branch as if we never branched away.

Get the graph with git log --oneline --graph --decorate:

git merge <branch>

  *__*__* new branch
 /                           --->
/ master (no commits here)          /*__*__* master (new branch merged)

Note: Fast-forwarding is only possible when there are no changes being made on the master branch.

Command Description
git merge <branch> --no-ff merge branch <branch> with the current branch (master) without fast-forwarding

Get the graph with git log --oneline --graph --decorate:

git merge <branch> --no-ff

  *__*__* new branch                  *__*__*
 /                           --->    /       \
/ master (no commits here)          / __ __ __\ master (new branch merged)

Branch Naming Convention

Note: Read Stackoverflow

Table of Contents

Rebasing

Command Description
git rebase <branch> branch is usually the master branch
git rebase <branch>

  *__*__* new branch                           *__*__* new branch
 /                        --->                /       
/ *__*__*__* master               / *__*__*__* master
Command Description
git rebase --abort abort and get back to the state before git rebase
git pull rebase pull and rebase commits on top of master
git pull rebase

/*__* master                  
                        --->                  
/ *__*__*__* master             / *__*__*__*__*__* master

Table of Contents

Stashing

Command Description
git stash stash the current changes
git stash -u also include untracked files by git
git stash list list of stashes
git stash drop drops a stash
git stash apply applies the stash on the current branch
git stash pop git stash apply and git stash drop in one command

multiple stashes

Command Description
git stash save "message" saves a stash with a message
git stash show stash@{1} looks at the second stash
git stash drop stash@{1} clears the second stash
git stash clear clears all stashes

Note: stashes have index and the last stash index is 0. e.g. 4 saved stashes:

stash@{0}
stash@{1}
stash@{2}
stash@{3}

stash@{3} is the first stash we made and stash@{0} is the last stash.

Stash into a Branch

Command Description
git stash branch <branch> creates a new branch <branch>, stashes into the new branch and drops the stash

Table of Contents

Tagging

Command Description
git tag <tag> assigns the tag <tag> to HEAD
git show <tag> shows changes of commit to which <tag> is assigned.
git tag --list shows the list of all tags
git tag --delete <tag> removes the tag with name <tag>

Annotated Tag

Annotated tag is equivalent to a commit message, but for tags.

Command Description
git tag -a <tag> -a means annotated tag. This tag also shows the message assigned to the tag when using git show <tag>
git tag -a <tag> -m <message> annotated message with message (does not pop up editor for message)

Other Commands

Command Description
git diff <tag1> <tag2> comparing tags
git tag -a <tag> <commit_id> tagging a specific commit
git tag -a <tag> -f <commit_id> updates the already defined <tag> on commit <commit_id>
git push origin master <tag> pushed the tag to the remote repository

Table of Contents

Reset and Reflog

Command Description
git reflog history of everything we have done
git reset <commit_id> moves HEAD to the <commit_id>

Note: git reflog is different with git log as it includes everything we have done. For example. git log does not show moving HEAD between different commits.

Cherry-Pick

Command Description
git cherry-pick <commit_id> cherry-pick a specific commit (<commit_id>)

Rewriting History

Update Last Commit

git ammend is the command to rewrite the history by changing the last commit or on your own branch at the public that no one has branched off of or is relying on the history of your branch. Because once you change that commit history, you are going to create a huge conflict with anybody who is relying on that history. You definitely never want to do this on master.

Note: Be careful of using git ammend. You only want to do this on a local branch

Command Description
git commit --amend -m <message> updates the last commit

Checkout to Orphaned Commit after ammend

  1. First get the commit id from git reflog.
  2. Checkout to the commit by git checkout <commit_id>.
  3. Branch away by git checkout -b <branch>.

Now HEAD is on the commit <commit_id>.

Exploring the reflog

Command Description
git reflog HEAD@{2.days.ago} reflog starting from 2 days ago
git reflog HEAD@{1.minute.ago} reflog starting from 1 minute ago

git-tutorial's People

Contributors

hejazizo 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.