Introducing Basic Git Commands
This article describes some of the basic commands that any new developer needs to know about to start using Git.
The first step when you want to work on an existing project is to clone the repository to your local file system. Open git bash and navigate to the location where you want to save the project, remember git creates a folder for the new repository that you are cloning, so usually you would navigate to the root of folder containing all of you projects, ex. C://Development/
The command you want to use is
git clone .
Existing Project Not From Source Control
If you have started working on a project and you have not added it to source control yet, you will have to add a relevant .gitignore for the project type you are using which can be found doing a google search. The next step is to execute the command
git init in the root folder of the project, unlike the clone command you are not creating a new folder, and you want only the project to be part of the git repository, therefore you will have to be inside of the folder of the project when you execute this command.
You will then have to execute the relevant commands to add the remotes manually and commiting the changes and pushing them to the remote repository.
If you manually initialized your local git repository there will be no remotes available. You will now have to manually add the remotes, the command for this is:
git remote add origin <RepositoryLocation>. This will create a new remote in your repository called origin, the name of the remote can be replaced with any other word that describes what you are doing. But the recommended name for this remote is origin.
Tracking Changed Files
The next step is to add the files to the repository that you want to track for changes, the easiest way is to execute the command
git add -A this adds all files in the working directory and sub directories. You can also specify exact files by specifying the file as an additional parameter by using the command
git add "
You have now created some files and made some changes, and you are tracking all of these changes. You now have to commit these changes to the repository by using the commit command:
git commit -m "Message", this command allows you to specify a commit message that describes the changes you made to the repository. Keep it short and to the point. I always find it best to do a commit for every feature added or changed or every bug fixed or issue resolved. This keeps the change history in a good order, and changes can be reverted easier.
Before you can push your changes to the remote, you first have to make sure that you have the latest files from the remote repository. To do this you can execute the command
git pull origin master this will then get the latest changes from the server and merge them with your local changes, if all goes well the merge should complete automatically, but in worst case scenario you will have to manually merge some of the files using a text editor or IDE.
Pushing your changes is the simplest of the commands but is one of the more important ones. If you do not push your changes there is no way for the files on the remote to be updated. To push your local commits to the remote you should use the command:
git push origin master
4 Step Sync
The quick and dirty way to use git is by doing the following four steps when you want to sync with the remote.
- Add Changes
- Commit Changes
- Pull Remote
- Push Remote