Setup
To initialise a Git repository (on CLI):
cd foldernavigate to the folder where the repository is to be madegit initinitalise an empty Git repositorygit statuschecks the status of the newly-created repository
Saving
Committing
Saves a snapshot of the current state of the tracked files in the revision control history
Staging
When ready to commit, add the specific changes to be committed to a staging area.
Tracking and ignoring
Files can be added to be tracked, or ignored.
General files that should be ignored:
- Binary files generated
- Temporary files
- Local files
- Sensitive content
Adding files Using CLI:
git add fileadds the file to be stagedgit statushere should show the changes to be committedgit commit -m "..."commits the changes with the message saidgit logshows commit history
Deleting previous commits
To undo the last commit, but keep changes in staging area: git reset --soft HEAD~1
To undo last commit, and remove changes from staging area: git reset --mixed HEAD~1
To delete last commit entirely (discard changes): git reset --hard HEAD~1
To undo/delete n in HEAD~n.
Ignoring files
- Create a
.gitignorefile. - Add the files to be ignored.
History
- each commit is identified by a unique hash
- each commit can be tagged with a easily identifiable name
git tag TAGNAME- to tag a previous commit,
git tag TAGNAME CHECKSUMwhere checksum is the hash (or part of it) of the previous commit to be tagged - to use annotated tags, use the option
-aand-mwhich specifies the tagging message
- to show what changed, use
git show CHECKSUM - to see changes between two points, use diff
git diffshows uncommitted changes since last commitgit diff CHECKSUM1 CHECKSUM2shows changes between the points indicated
- to restore the state of the working directory, checkout the commit in concern.
git checkout TAGloads state as at commit taggedgit checkout CHECKSUMloads state at commit with matching checksumgit checkout HEAD~nkiads state that iscommits behind most recent commit.
- to stash changes means to temporarily shelve changes made currently so that something else can be worked on
git stashstashes all uncommitted changes (both staged and unstaged) and saves them away for later usegit stash popreapplies previously stashed changesgit stash applyreapplies previously stashed changes, but keeps them in the stash, which is useful for applying same changes to multiple branches-uindicates to stash untracked files-aindicates to include ignored filesgit stash save MESSAGEsaves the stash with a messagegit stash listshows the list of stashes.
- for branching
git statusshows what branch currently on (should showon branch BRANCHNAME)git branch NEWBRANCHNAMEcreates a new branch with the nameNEWBRANCHNAMEgit switch BRANCHNAMEswitches to the branch with the nameBRANCHNAMEgit checkout BRANCHNAMEdoes the equivalent
git switch -c NEWBRANCHNAMEcreates a new branch and switches to itgit checkout -b NEWBRANCHNAMEdoes the equivalent.
git merge BRANCHmerges the branchBRANCHinto the current branch.- to push a remote branch,
git push -u origin BRANCHNAME-uspecifies local branch should track remote branch created as a result of the push
- to pull a remote branch,
git fetch REMOTEandgit branch -ato see the details of the branchesgit switch -c BRANCH REMOTE/BRANCHto create a matching local branch and switch to it
- to push new changes in local, omit the
-uflag:git push origin BRANCHNAME - to pull new changes to local,
git switch BRANCH, thengit pull origin BRANCH.
Remote repositories
Cloning creates a copy of the repo in another location on computer. The original repo cloned from is then referred to as an upstream repo for the repo cloned on the computer.
git clone URL
A repo can be pulled/fetched from to receive new commits in the second repo if the repos have a shared history.
git pull origingets missing commits in the local repo from the upstream repogit fetch origingets the missing commits but does not move the current state to the latest downloaded commit- To communicate with another remote repo,
git remote add REMOTE_NAME REMOTE_URLwhich adds a remote repo. This can then be pulled/fetch from by specifyinggit fetch upstream master; git merge upstream1/masterorgit pull upstream1 master
New commits can also be pushed to another repo if repos have a shared-history, and write access is had.
git push origin TAGpushes a specific taggit push origin --tagspushes all tags
A fork is a remote copy of a remote repo.
- not a Git feature, but a Github feature
A pull request is a mechanism for contributing code - it is a request to pull the changes made by someone to someone else’s repo.