Git
Version Control System to track the history of changes made to a project.
Basic Git Commands:
git init
initializes a brand new Git repository and begins tracking an existing directory.
git clone
creates a local copy of a project that already exists remotely.
git add
stages a change. Any changes that are staged will become a part of the next snapshot and a part of the project's history.
git commit
saves the snapshot to the project history and completes the change-tracking process.
git status
shows the status of changes as untracked, modified, or staged.
git branch
shows the branches being worked on locally.
git merge
merges lines of development together.
git pull
updates the local line of development with updates from its remote counterpart.
git push
updates the remote repository with any commits made locally to a branch.
When you
git clone
, git fetch
, git pull
, or git push
to a remote repository using HTTPS URLs on the command line, Git will ask for your GitHub username and password. When Git prompts you for your password, enter your personal access token.Stage and snapshot
git status
shows modified files in working directory, staged for your next commitgit add [file]
adds a file to your next commitgit reset [file]
unstages a file while retaining the changes in working directorygit diff
shows differences of what is changed but not stagedgit diff --staged
shows differences of what is staged but not committedgit commit -m “[descriptive message]”
commits your staged content as a new commit snapshotBranch and Merge
git branch
shows the branches being worked on locally.git branch [branch-name]
creates a new branch at the current commit.git checkout
switches to another branch and check it out into your working directory(more details about git checkout: https://git-scm.com/docs/git-checkout)
git merge [branch]
merges the specified branch’s history into the current onegit log
shows all commits in the current branch’s historyInspect and compare
git log
shows the commit history for the currently active branchgit log branchB..branchA
shows the commits on branchA that are not on branchBgit log --follow [file]
shows the commits that changed file, even across renamesgit diff branchB...branchA
shows the differences of what is in branchA that is not in branchBgit show [SHA]
show any object in Git in human-readable formatShare and update
git remote add [alias] [url]
adds a git URL as an aliasgit fetch [alias]
fetches down all the branches from Git remotegit merge [alias]/[branch]
merges a remote branch into your current branch to bring it up to dategit push [alias] [branch]
transmits the local branch commits to the remote repository branchgit pull
fetches and merge any commits from the tracking remote branchTemporary commits
git stash
saves modified and staged changesgit stash list
lists stack-order of stashed file changesgit stash pop
writes working from top of stash stackgit stash drop
discards the changes from top of stash stackGetting a repository on your local machine
Two ways to obtain a git repository:
- Initializing a repository in an existing directory - take a local directory that is currently not under version control, and turn it into a Git repository.
- You can clone an existing Git repository
The 1st method: Initializing a repository in an existing directory
Go to the directory you want
cd /your_directory
type git init
This creates a new subdirectory:
.git
that contains all of your necessary repository files but nothing is tracked yet. To begin tracking those files, do an initial commit:git add *
git commit -m 'Initial commit'
The 2nd method: Cloning an existing repository:
git clone https://github.com/YOUR-USERNAME/YOUR-REPOSITORY
If you want to clone the repository into a directory with a different name, specify the new directory name as an argument
git clone https://github.com/YOUR-USERNAME/YOUR-REPOSITORY myproject