Install git, check (https://git-scm.com/book/en/v2/Getting-Started-Installing-Git) for easy and clear installation.
After installation, ensure you configure your username and email before making any commit. It allows commits to have right author name and email associated to them. However, this has nothing to do with authentication when pushing to a remote repo.
Git config --global user.name "my_username"
Git config --global user.email firstname.lastname@example.org
The above commands declares identity for all repositories, to declare an identity for a single repository, use git config inside a repo
cd my_repository git config user.name "my_work_login" git config user.email email@example.com
Remove a global identity
git config --global --remove-section user.name git config --global --remove-section user.email
However, to force git to look for your identity only within a repository's settings, not in the global config
git config --global user.useConfigOnly true
Tip: Settings stored in a repository's config file will take precedence over the global config when you use that repository
git --version - verifies if git is installed
git init – initiates or creates an empty repository. It creates a hidden folder(.git) necessary for git to work
git clone<project_name> - copies an existing Git repository from a server to the local machine. It is followed by a url for the repository that is being cloned.
Example: git clone https://github.com/my_username/my_project.git
NB: You can also use the ssh version of the command:
git clone firstname.lastname@example.org:my_username/my_project.git
However, GitHub recommends using https rather than ssh.
git status - checks what files Git will add to your new repository
git add <file> - tells Git which of the files to place into version control
git add . - tell Git place everything in the directories and the sub-directories into version control.
git reset <file> - unstage a file while retaining the changes in working directory
git diff - difference of what is changed but not staged
git diff --staged - difference of what is staged but not yet committed
NB: create and populate a file named .gitignore before running the add command for files that you want to exclude from being tracked by Git
git commit -m "Initial commit" – commits all the added files
A commit creates a snapshot of your entire project. After a commit, the project is ready to upload to a remote repository
git remote add origin <https://<your-git-service-address>/owner/repository.git>
NB: You have to create a repository in your git service before adding the created remote.
git branch - list your branches. a * will appear next to the currently active branch
git branch [-a] - List all local branches in repository.With -a: show all branches(with remote).
git branch -d [name] - Remove selected branch, if it is already merged into any other. -D instead of -d forces deletion.
git checkout [-b][branch_name] - Switch working directory to the specified branch. With -b: Git will create the specified branch if it does not exist.
git branch <branch-name> - create a new branch at the current commit
git checkout - switch to another branch and check it out into your working directory
git merge <branch> - merge the specified branch’s history into the current one
git log branchB..branchA - show the commits on branchA that are not on branchB
if you clone a fork, most probably you will not be able to push access to the upstream repo, so you need both your fork but be able to fetch the upstream repository.
git remote -v - check the remote names
git remote -v origin https://github.com/myusername/repo.git (fetch) origin https://github.com/myusername/repo.git (push) upstream # this line may or may not be here
If upstream is there already, set the URL
git remote set-url upstream https://github.com/projectusername/repo.git
if it is not there, or you want to add a friend’s fork
git remote add upstream https://github.com/projectusername/repo.git git remote add emma_donery https://github.com/emma_donery/repo.git
The help command gives details about what the command does, available options and
git command_name --help
git push --help
git remote add [alias] [url] - add a git URL as an alias
git fetch [alias] - Fetch down all the branches from that Git remote
git merge [alias]/[branch] - merge a remote branch into your current branch to bring it up to date
git push [alias] [branch] - Transmit local branch commits to the remote repository branch
git pull - fetch and merge any commits from the tracking remote branch