Git
Git sands for Global Information Tracker and powerful version control system widely used for software development and other collaborative projects.
In this post we are going to give a short description of Git’s command on command line and in Visual studio.
What is Git
Git is a version control system used for tracking changes in computer files. It is generally used for source code management in software development.
- Git is used to tracking changes in the source code
- The distributed version control tool is used for source code management
- It allows multiple developers to work together
- It supports non-linear development through its thousands of parallel branches
Installation of Git
Git can be installed on the most common operating systems like Windows, Mac, and Linux. In fact, Git comes installed by default on most Mac and Linux machines!
Git can be downlaoded and installed from the following link: Getting-Started-Installing-Git
You can downloads and install git for Windows from here
Git Commands
followings are git commands on command line
- git init: creates a new Git repository. It can be used to convert an existing, unversioned project to a Git repository or initialize a new, empty repository. Most other Git commands are not available outside of an initialized repository, so this is usually the first command you’ll run in a new project.
- git colone: command is used to create a copy of a specific repository or branch within a repository. Ecample: git colone <url> as follow.
git clone https://github.com/github/training-kit.git
- git branch <branchName>: to create a new local branch with name branchname e.g. feature to work it.(check it by command: git branch -a ) Once created you can then use git checkout new_branch to switch to that branch to work with it. it is better to do a local branch and work with it and merge it to the main (Master local branch) when you are ready.
- git add: adds a change in the working directory to the staging area. It tells Git that you want to
include updates to a particular file in the next commit. Before we make a commit, we must tell Git what files we want to commit (new untracked files, modified files, or deleted files). This is called staging and uses the add command. Why must we do this? Why can’t we just commit something directly? Let’s say you’re working on two files, but only one of them is ready to commit. You don’t want to be forced to commit both files, just the one that’s ready. That’s where Git’s add command comes in. We add files to a staging area, and then we commit what has been staged. Even the deletion of a file must be tracked in Git’s history, so deleted files must also be staged and then committed. Read More - git status : You’ll see what branch you are on (which for new repos will be master) and status of files (untracked, modified, or deleted). We’ll explain branches later.
- Stage Files to Prepare for Commit:
- Stage all files: git add .
- Stage a file: git add example.html (replace example.html with your file name)
- Stage a folder: git add myfolder (replace myfolder with your folder path)
Keep in Mind:
- If the file name/path has a space, wrap it in quotes.
- You can repeat the above commands for different files and folders.
Check the status again by entering the git status
- git restore –staged <file>: to unstage file
- git stash: temporarily shelves (or stashes or hides) changes you’ve made to your working copy so you can work on something else, and then come back and re-apply them later on. Read more here
- git stash pop: (alternative git stash apply): You can reapply previously stashed changes with this command, Popping your stash removes the changes from your stash and reapplies them to your working copy.
- Unstage a File: by command: git reset HEAD example.html
(replace example.html with your file or folder) - git rm: deletes a file and stages it all with one command. e.g: git rm example.html to remove a file (and stage it) or git rm -r myfolder to remove a folder (and stage it)
- git commit -m “Message that describes what this change does” commits your stage area (one or several files not in the other modified files)
- git branch <branchName>: to create a new local branch with name branchname e.g. feature to work it.(check it by command: git branch -a ) Once created you can then use git checkout new_branch to switch to that branch to work with it.
- git checkout feature: switches tp to the feature branch which you can work with it.
- git merge feature: this merge feature branch to your main branch (master branch) locally. before this you should checkout the master branch by command: git checkout master for master command that to do off for feature branch.
- git branch –delete <branchname>: deletes a local branch with name branchname. (if necessary, use the git switch or checkout command to move off the branch you wish to delete this means that you can do git checkout master). Run the git branch -a command to verify the local Git branch is deleted (e.g. git branch –delete feature deletes feature branch)
- git push : this command is used to upload local repository content to a remote repository. Pushing is how you transfer commits from your local repository to a remote repo. It’s the counterpart to git fetch command. Read more
- git fetch : Fetch all of the branches from the repository. This also downloads all of the required commits and files from the other repository. Read more
- git pull: The
git pull
command is used to fetch and download content from a remote repository and immediately update the local repository to match that content. Read more - git commit –amend: merges your changes into your last commit instead of storing it as a new commit ( git commit –amend -m “ammend last commit”)
10 Advanced Git Techniques
10 important Git command: look to the 10 Advanced Git Techniques
Git on Visual Studio
You can install git on visual studio and then you can go to the Tools: Options : Source Control and in the right side select git under Source control plugin: as follow:
After that you can see the Git menu in the main menu of Viusal studio as follow:
From the above figure you see all commands needed to use for example: Colone Repository, Commit or stash, ..
In the in the Solution explorer change some files and go to the View: click on the Git change as follow:
then it shall show the changes Git Changes UI as follow:
As you see in the above figure it shows Changes 1 and Stage changes 1.
If you click on the + in the right side of changed file then it adds it to Stage changes
If you click on the – in the right side of Stage changes it shall move to the Changes files.
You can first stage a file and after that write a comment and press to the commit Staged button.
Creating branch in VS
In the bottom of Solution Explorer you can find the branch name “master). Click to it then it is opens a new window and shows all branches as shown in the following:
You can press to New Branch and create a new branch as Feature branch which is created in the figure above.
All commands are similar the git commands which are describe in the Git commands section of this post.
Amend checkbox in Visual Studio: Git Change window: if you select the checkbox amend in VS and then press to commit then the cuttent changes shall be mergied with the last commited.
Conclusion
In this post we have showed the Git’s important commands, how they works. We have also showed Git on the Visual Studio and how the command on this works.