Pushing, Pulling, fixing Merge issues among many others, used to be insurmountable problems for me because I was not familiar with git. I was always scared that I would ruin something along the way, and cause my code to break or just disappear completely. It took a while getting to understand how these processes work. After getting a grasp of some of it, I thought I should share the knowledge.
If you do not already know why Git is important, know this:
Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency.
I’d talk about 10 Git commands that would help you work effectively.
1. git init
This is basically the starting point for you. You would need to choose a location on your system which would be used as your local Git repository. The 'init' command would create a new git repository for you. If you already have an existing project, 'init' would create a git repository in your working directory. On your terminal, cd into the directory path where your project is, and type git init.
$ cd var/www/devProj $ git init
2. git clone
If you are working in a team, and you need to work on an already existing repository, you might need to clone it so as to have a version on your local. Git clone simply replicates the content of an existing repository, and reproduces it in a new directory. On your terminal, cd into the the directory path where you would want the project to be, and clone it there.
$ cd var/www/ $ git clone path/to/the/repo
3. git add
If you have been working on a branch, you would need to add the files you have been working on to your index before you can stage for a commit. You can add a specific file using
git add or you can add all your new and modified files at once using
git add . You can git add several times before committing, as long as new changes are being made.
$ git add /name/of/file $ git add . //new and modified files
4. git commit
After you have added all the changes, you can stage all of them for a commit. This updates the Head but not the remote repository. Every time you use
git commit,what you are doing is to collate all the file changes you have earlier recorded using
git add .,and making it into an object.
$ git commit -m "write-a-commit-message" //You can actually add and commit at once using - $ git commit -am "write-a-commit-message"
5. git branch
Git branch shows a list of all the branches available in a repository. It also shows what branch you are presently on. A branch is a portion of a project that is independent of all others.Your first commit is usually made to Git’s default branch,
master. If there is a need to create a new branch, you can use git branch new-branch. This new branch would be independent of all other existing branches until it is merged into another branch.
$ git branch branch-name $ git branch -D branch-name //This deletes the created branch
6. git checkout
Git checkout is necessary when you want to leave a branch. Recall that git branch only creates a new branch, however, git checkout moves you to an already existing branch.
$ git checkout branch-name //This switches you to another branch $ git checkout -b new-branch-name //This creates a branch and checks out to it also.
7. git status
This command tells you what branch you are currently on. It identifies all the changes that have been made, and all the changes that have not yet been added and committed to that branch.
$ git status
8. git log
It lists all the previous commits made on that branch, the author, the time and the date of commit. This helps you keep track your activities.
$ git log
9. git push
If there are several people working on a project,the several others would definitely need to work with the new changes you have just made on a branch. For them to access your changes, you would need to push your commits to the remote repository.This way, the remote version would be updated, and can be easily accessed by others.
$ git push origin master
10. git pull
As a developer, you might need to pull from a couple of repositories.Pulling takes files from a remote source and merges them to your local files. That way, you would have access to the same file contents as the remote repository.
$ git pull origin master
There are a lot of other git commands, and usually, you would find them out when you exactly need them. Nevertheless, the above listed git commands would definitely be of help to you especially if you are just getting acquainted with git.
No one knows it all, so, if there are other commands you would like to share, please don’t hesitate to drop them as comments.
If you like the article, please like and share.