by Aditya Bhelande · July 2, 2019 · in Software Development
Git is a version control system (VCS) for tracking changes in project directory and manage source code. It is free and open source. A complete long-term change history of every file. This means every change made by many individuals over the years. Git takes snapshots of a project, and stores those snapshots as unique versions. Git is designed for distributed development. If you’re involved with a project you can clone the project’s Git repository, and then work on it as if it was the only copy in existence. Every developer’s working copy of the code is also a repository that can contain the full history of all changes.
If you want to install the basic Git tools, go through Git installation URL There are many interfaces available for Git like GitHub, GitLab, Savannah, BitBucket, and SourceForge etc.
To start with Git need to make project directory as Git repository usually we called it as ‘Local Repository’.
Initializing a new repository: git init
To create a new repo, you’ll use the git init command. git init is a one-time command you use during the initial setup of a new repo. Executing this command will create a new .git sub-directory in your current working directory. This will also create a new master branch.
Add a remote repository.
|git remote add origin ssh://email@example.com/[username]/[repository-name].git|
Git setup command:
After initializing Git in the directory you should link your username and email address to your Git profile. You can use the following command to do the same.
|git config –global user.name “FIRST_NAME LAST_NAME”|
git config –global user.email “MY_NAME@example.com”
There are the level of configuration we can do, one is with –global option which basically works as the default configuration for all git repository setup on your system. Another one is with –local option which only applies to the repository where you are running this command.
|git config user.name “FIRST_NAME LAST_NAME”|
git config user.email “MY_NAME@example.com”
Basic Git commands that usually needs to perform Git operations:
To Cloning an existing repository: git clone.
|git clone /path/to/repository|
It used to create a copy or clone of remote repositories. When executed, the latest version of the remote repo files on the master branch will be pulled down and added to a new folder.
Local changes commands:
- If files were modified then it will shows the list of files/directories which gets modified. Usually used to check the status of project files whether they modified or not.
- Check the changes to tracked files. Used to check the difference between the last version of files and the current version of modified files.
- Add current changes to the next commit. Used to add the newly added or modified files to current branch.
- To add all new and changed files to the staging area.
- Commit staged local changes in tracked files. Used to commit the recently added files to the branch and save changes to project/repository. Options we can use with : ‘-m’ to specify commit message.
- To remove a file (or folder).
|git rm -r [file-name.txt]|
- To store our changes when they are not ready to be committed and we need to change to a different branch.
- Apply stash changes/files and drop on one command:
- If you added a file by mistake, you can unstage it (but keep local changes).
|git reset HEAD path/to/file|
- To check current branch and to display the git branch names list available.(the asterisk denotes the current branch).
- To list all existing branches.(local and remote)
- To switch from one to another branch.
|git checkout <branch name>|
- To create and switch into new branch.
|git checkout -b <new-branch name>|
- To remove all the modified changes from the specified file.
|git checkout <fileName/path>|
- To clone a remote branch and switch to it.
|git checkout -b [branch name] origin/[branch name]|
- To delete the branch from local repository.
- To delete a branch on the remote.
|git branch -dr <remote/branch>|
git push origin –delete [branch name]
Git merging and rebase:
- To merge a branch into the active branch. It simply merges one branch into parent or master branch. When your new feature is ready, you might want to integrate it into another branch (e.g.: prod or testing branch).
First, switch to the branch that is supposed to receive these changes. Then call “git merge” command with the name of the branch you want to integrate.
- To integrate changes from one branch into another. It is an alternative to the better known “merge” command.
- Used to abort the rebasing operation.
- To Continues the rebasing process only when all the conflicts get resolved.
UPDATE & PUBLISH Commands:
- To pull the project/files/code from the remote location and to download and integrate remote changes. To integrate new changes from the remote repository. This will update your current HEAD branch with the new data from its counterpart branch on the remote. The changes will be directly merged into your local working directory.
- To publish the committed changes on remote location. Options we can use with : ‘-f’ to forcefully publish changes.
|git push <remote> <branch>|
UNDO git operations:
git reset changes, where your current branch is pointing. The difference between –hard and –soft is whether or not your index is also modified. So, if we’re on branch master with this series of commits: – A – B – C (master)
HEAD points to C and the index matches C.
When we run git reset –soft B, master (and thus HEAD) now points to B, but the index still has the changes from C; git status will show them as staged. So if we run git commit at this point, we’ll get a new commit with the same changes as C.
It modifies your working directory. If we’re at C and run git reset –hard B, then the changes added in C, as well as any uncommitted changes you have, will be removed, and the files in your working copy will match commit B. Since you can permanently lose changes this way, you should always run git status before doing a hard reset to make sure your working directory is clean or that you’re okay with losing your uncommitted changes.
- To Discard all local changes in your working directory. It removes all the files changes.
- To Discard local changes in a specific file. Restores all the changes to file up to the last commit.
- Reset your HEAD pointer to a specific commit and discard all changes since then. Removes all the changes done after the commit ID specified in the command.
- To remove the commit but keep the committed changes to the staged area on the local repository.
COMMIT HISTORY Commands:
- Show all commits, starting with latest commit information.
- Show changes over time for a specific file.
- Who changed what and when in <file>.
- Preview changes before merging.
|git diff [source branch] [target branch]|
Version Control Best Practices:
- Commit Related Changes: A commit should be a wrapper for related changes. For example, fixing two different bugs should produce two separate commits.
- Commit Often: It allows you to share your code more frequently with others. That way it’s easier for everyone to integrate changes regularly and avoid having merge conflicts.
- Test Before You Commit: Resist the temptation to commit something that you “think” is completed. Test it thoroughly to make sure it really is completed and has no side effects (as far as one can tell).
- Write Good Commit Messages: Begin your message with a short summary of your changes.
- Use Branches: Branches are the perfect tool to help you avoid mixing up different lines of development. You should use branches extensively in your development workflows: for new features, bug fixes, experiments, ideas…
If you have any queries, questions or suggestions, feel free to share on firstname.lastname@example.org
Happy to help!