To Install latest Git software https://git-scm.com/downloads After installing for the 1st time, we need to define/configure user name and user email Syn: git config –global user.name “username” Syn: git config –global user.email “email id”
- Clone the project if existing project else ignore this step
- Initialize
- Add
- Commit
- Push
- Pull
- Branching
- Merging
- Rebasing
- Create new repository.
- To create the connection between local to server a. Git Init b. Git add c. Git commit
**Git remote ** Used to connect between local and server Syn: git remote add origin For the first time we need to setup the upstream branch Syn: git push –set -upstream origin master Will set the upstream branch as master branch in remote.
Git Clone: To clone the code to the local Or to download the project from remote to local. Syn: git clone
Git Status: To know the changes between the local and server. Syn: git status
Git restore: Will restore the local changes to last pulled state. Syn: git restore
Git add: This cmd is used to add local changes to stage. Syn: Git add . (to add all changes to stage)
Git Status: To know the changes between the local and server. Syn: git restore –stage command used to unstage the changes.
Git Commit: To commit the changes Syn: git commit “message/comments”
Git Status: Syn: git status will return the current state of the files are committed to the respective branch.
Git Push: Syn: git push to push the changes. Note: If authentication is not happened then system will ask for authentication, give token or login into the git Now code will move to the branch.
Git Checkout: Used to change the branch Syn: git checkout <branch_name>
Git branch: Will retrieve the current branch name Syn: git branch
We will create branch as per the feature level and after review merge/pull to the develop branch.
Then, final review will happen and if no conflicts the code will be merged with master branch.
Note: master branch will be used to run the automation scripts. Note: master branch will be protected, we can changes properties in git.
Git Conflict: If multiple users are working on common files, while pulling the code before pushing the changes git will raise conflict. While pulling the changes git will show error as conflicts Option1: Right click on project Git resolve conflicts compare the changes and do necessary changes OR OR OR We can do using stash concepts
- Branching Strategy
- Follow Branching Convention
- Discard the changes if not needed (use git restore command to revert the changes)
- Track the differences in the files (git diff to see the differences or Git Menu Commit select the file and visually we can see the changes)
- Pull the changes from remote before pushing the latest changes.
- Push the changes to remote regularly.
- Stash the changes if needed (to store the changes temp ). a. “Git stash list” to get the list of stashed items b. “Git stash apply” to apply all changes c. “Git stash apply ” to apply the respective change
Stash: Stash is to store the changes temporarily. Syn: git stash Syn: git stash Syn: git stash list (will retrieve all the stashed records) Syn: git stash clear (to clear all the stashed records) Syn: git stash apply (to revoke the stashed records)
Commit: Commit is to store the changes permanently. Syn: git commit Syn: git commit
Committed: Already committed to server
Un-committed: changes are not committed to server. How to UNDO un-committed changes
- Git restore
- Git restore-staged
- Git checkout
Git Restore: Used to remove the changes which are not committed Syn: git restore Syn: git restore .
Git restore-staged: Used to revert the staged changes Syn: git restore –staged After this use command Syn: git restore .
Git Checkout Used to checkout the branches, using this we can revert the changes Syn: git checkout
We can achieve this by using the below commands. Using Git Reset Git reset –soft HEAD~ Git reset –Hard
Git Log: To track all the committed changes Syn: git log (will retrieve all the committed logs ) Syn: git log –oneline (will retrieve the details in single line and it is readable)
Git reset –soft: Used to revert the local commits, but it will keep the changes in stage. After this use git restore to remove changes permanently. Syn: Git reset –soft HEAD~ (latest commit) Syn: Git reset –soft HEAD~2 (will revert the last two commits)
Git reset –Hard: Used to revert the local commits, but it won’t give any option to do or see the changes. It will restore as well. Syn: Git reset –Hard
Syn: git revert <commit id> and we need to quit by adding comments
Syn: git revert <commit id> --no-edit (we can do without adding any comments)
Merge: Preserves the history and log All commits are combined into a single commit Usage: when target branch is public then we use Merge Rebase: Rewrites the history and create a linear log All commits are rebased and same number of new commits are added. Usage: when target branch is private then we use rebase.
The key difference between git fetch and pull is that git pull copies changes from a remote repository directly into your working directory, while git fetch does not. The git fetch command only copies changes into your local Git repo.
Git Fetch:
Used to fetch all changes from the remote repository to the local repository without merging into the current working directory Repository data is updated in the .git directory Review of commits and changes can be done No possibility of merge conflicts. Command for Git fetch is git fetch
Git Pull: Brings the copy of all the changes from a remote repository and merges them into the current working directory The working directory is updated directly Updates the changes to the local repository immediately. Merge conflicts are possible if the remote and the local repositories have done changes at the same place. Command for Git Pull is git pull