The git pull command merge upstream changes into your local repo. git pull rolls git fetch and git merge into single command.
#fetch the specific remote copy of local branch and merge it into local copy.
#Same as the above command, but instead of using git merge to integrate the remote branch with the local one, use git rebase
git pull --rebase
Pulling vs Rebase:- The –rebase option can be used to ensure a linear history by preventing unnecessary merge commits. Many developers prefer rebasing over merging, since it’s like saying, “I want to put my changes on top of what everybody else has done.”
git config --global branch.autosetuprebase always
After running that command, all git pull commands will integrate via git rebase instead of git merge.
GIT FETCH:- The git fetch command imports commits from remote repo to your local repo.he resulting commits are stored as remote branches instead of the normal local branches that we’ve been working with. This gives you a chance to review changes before integrating them into your copy of the project.
#Fetch all of the branches from the repository. This also downloads all of the required commits and files from the other repository.
#Same as the above command, but only fetch the specified branch.
#This will display the branches that where downloaded
git fetch origin
#This will download all branches to your local
git fetch --all
The git stash command takes your uncommitted changes (both staged and unstaged), saves them away for later use, and then reverts them from your working copy.
#to stash changes
#Reappling your stashed changes
git stash pop
#you can reapply the changes to your working copy and keep them in your stash(This is useful if you want to apply the same stashed changes to multiple branches)
git stash apply
By default running git stash command stash your working directory files only to stash untracked or ignored file use -u option
git stash -u
git stash --include-untracked
You are not limited to do single stash. you can run git stash several times then use git stash list to list out all stash.
GIT COMMIT:- Git commit command commits the staged snapshots to the project history. Whenever you do git commit it will commit the changes into staged area but not on server to send your changes to server you should you git push command.
To commit specific files we should use file path with commit. always be sure before using -a (all) flag. This will commit all files some may be needed some may not for your repository. -m flag is always required if you don’t pass it in command it will open your preferred editor asking for the message. If any file is new you should add file first using git add command. We can use git status command at any time to see what the project status is. –amend flag is very useful when you forgot to commit any file this will add the current files is last commit with same message as last commit is. you can also change the message.
Git Commit Command
#commit staged snapshot
#commit staged snapshot without launching the text editor asking for message
git commit -m
#commit all changes to working dirctory with all modified files.
git commit -a
#add the changes to last commit
git commit --amend
git add commands add changed file to working directory to the staging area. It tells Git that you want to include updates to a particular file in the next commit. However, git add doesn’t really affect the repository in any significant way—changes are not actually recorded until you run git commit.
# add a single file
git add file_name
#add directory to repo
git add folder_name
GIT CONFIG:- git config command lets you configure your git installation from the command line. the command can define everything from user info to preferences to the behavior of repo. Several commands are available as follow
git config --global user.name //define a user name used for all commits
git config --global user.email //define a user email used for all commits
git config --global alias. //create a shortcut for git command
git config --system core.editor //define text editor used by commands like commit
git config --global --edit //To open global configuration file.
Some examples are given below
GIT CLONE:- clone command is used for cloning the already existing repo to local machine.
git clone // clone a repo
git clone folder_name // clone repo to particular folder
GIT INIT:- git init command is use for creating a new repo. it can also be used to convert existing unversioned project to git repo or initialize a new empty repo.
By exicuting git init command it creates a .git sub-directory in the project root. which contains all necessary meta-data for the repo. Unlike svn git doesn’t requires .git folder in each sub-directory.
git init // new git repo
git init //transform the directory into git
git init --bare // this will create a new directory and .git as its subdir.
Install Git (Linux/Ubuntu)
From your shell install git using apt-get
sudo apt-get update
sudo apt-get install git
verify the version of git after installation
Configure a git username/email by following command
git config --global user.name "Emma Paris"
git config --global user.email "email@example.com"