Git - AshokBhat/notes GitHub Wiki
Git
- Notes
- Terms
- Remote
- Remote repository which local repo is linked to.
- Local repo
- Repository that is stored locally
- Staging
- Intermediate place where files/changes are held before committing to local repo.
- Working
- Actual files on which changes are being made.
- Staged changes
- Changes that have been added to staging but not committed yet.
- Unstaged changes
- Changes that have been made to working but not added to staging area.
- HEAD
- Like a pointer to current branch
- Stash
- Temporary location where un staged changes can be kept without staging.
- Idea is to quickly save some changes that you're not ready to commit or save, but want to come back to while you work on something else.
- Remote
- Merging process
- When one branch is merged onto another, git merges all the commit snapshots of both branches into one tree.
- The individual snapshots are merged on the basis of time when commits were made.
- In addition, a final 'merged' commit snapshot will be made which contains all the merged files.
- If merge cannot do be done automatically, git notifies conflict exists.
- User has to resolve the conflict and add the commit the changed files.
- At this stage, entire merge history will be created.
- Terms
- Creating Projects
- Create git repo in current directory
- git init
- Create git repo in current directory
- Getting projects
- Clone a remote repo
- git clone
- Clone a remote repo
- Basic operations
- Adding file to staging
- Add a file
- git add
- Add all modified and removed tracked files
- git add -u
- Add a file
- Commit staging to repo
- Provide commit message using editor
- git commit
- This will open up and editor where you can provide commit message.
- Commit message with command
- git commit -m
- Provide commit message using editor
- Remove file
- From staging
- git reset HEAD
- Removes file from the staging area
- File is not physically removed
- git status will not show that file is in staging area ready to be committed.
- git reset HEAD
- From repo and local storage
- git rm
- Removes file from local storage
- File will be shown as to be deleted in staging area.
- git rm
- From staging
- Show difference
- Show unstaged differences (between unstaged and repo)
- git diff
- Show staged differences (between staging and repo)
- git diff --cached
- Show all differences (unstaged and staged)
- git diff HEAD
- Show unstaged differences (between unstaged and repo)
- Adding file to staging
- Advanced operations
- Amend last commit
- git commit --amend
- Change last commit with new commit message and/or files.
- git commit --amend
- Remove last commit
- git reset --hard HEAD^
- Change previous commits
- git commit --fixup <commit_id_to_amend>
- git rebase -i --autosquash
- Find who modified each line of file.
- git blame
- Show what revision and author last modified each line of a file
- git blame
- Amend last commit
- Branches
- List branch
- Local
- git branch
- Remote tracking
- git branch -r
- All
- git branch -a
- Verbose
- See the last commit on each branch
- -v option
- Some Variants
- git commit -va
- All branches + Verbose
- git commit -vr
- All remote tracking branches + Verbose
- git commit -v
- All local branches + verbose
- git commit -va
- Local
- Create branch
- Copy of current branch
- git branch
- Copy of named branch
- git branch
- Copy of current branch
- Switch to another branch
- Existing branch
- git checkout
- New branch, which is copy of current branch
- git checkout -b
- New branch, which is copy of another branch
- git checkout -b
- Existing branch
- Delete branch
- Local branch
- git branch -d
- Remote branch
- git push (remote-name) :(branchname)
- Local branch
- Diff
- Two branches
- git diff
- Two branches
- Merge
- Merge another branch into current branch
- No conflict
- git merge
- Conflict
- git merge
- Resolve conflict
- git add
- git commit
- No conflict
- Merge another branch into current branch
- List branch
- Working with remote repos
- Update local repo with remote, but not merge
- git fetch
- Update local repo and merge
- git pull
- Push to remote
- git push
- e.g
- git push origin master
- Update local repo with remote, but not merge
- Stash current work and pop it back
- Stash - add current changes to the stack
- git stash
- View stashes currently on the stack
- git stash list
- Apply stash on top of the stack
- git stash pop
- Apply stash from items on stack
- git stash apply
- example git stash apply stash@{0}
- git stash apply
- Drop a stash
- git stash drop
- example git stash drop stash@{0}
- git stash drop
- Drop all stash
- git stash clear
- Stash - add current changes to the stack
- Patches
- Create patch from last commit
- git format-patch -n HEAD^
- Create series of patches
- git format-patch
- Create a patch file for each commit that happened since the specified commit <last_commit_id>
- git format-patch
- Create patch from last commit