If you work on software development project then you might have already used git for version control. If you haven't used git yet then it's high time you should learn and start using this. Following are the 10 git commands you must know while getting started with git.
1. git init: It initializes the git repository. Running this command creates a directory(.git) inside the current directory which contains git configuration and other repository data. See below image how there was no git repository initially and after running git init, an empty git repository is initialized as we can see the .git directory gets created.
2. git status: It shows the current status of the repository by showing the difference between index file and the current HEAD commit. It shows the untracked files and files which are added to staging area but not yet committed. You can use different options with git status.
Using -s option gives the output in short format.
Option -v or --verbose will show the changes which are staged to be committed.
3. git checkout: It is used for switching between branches.
git checkout <branchName> will switch to the branch. You can also create a new branch and then switch to it in one step by using the option -b.
git checkout -b <branchName>creates a new branch and then switches to the newly created branch.
4. git add: It adds files to the staging area to be committed.
git add <file1> <file2> will add files "file1" and "file2" in the staging area. We can also use regular expressions to add multiple files at once.
git add *.txt will add all files with suffix .txt.
git add . will add all files present in the current working directory.
5. git commit: It commits all the changes which are in the staging area. Running the command git-commit will open the text editor for the commit message to write. Write useful commit message so that even other contributors to the repository should be able to figure out about the changes done in the particular commit.
git commit -m "commit message goes here": By using the option -m, you can directly provide the commit message in the same line without opening a separate editor.
git commit -a : It will add all changes to staging area then commit.
git commit -am "commit message": Adding all files and committing inline.
6. git diff: It shows the changes made since the last commit. To see the changes in the file which are added in the staging area, use git diff --cached.
7. git log: It shows commit logs.
8. git branch: It helps in showing the current working branch.
git branch -d <branchName> can be used to delete the branch.
9. git stash: If you made some change in a branch but want to park those changes for a while and want to work on some different branch, git stash is your friend. It parks the uncommitted changes to a stash area. You can have multiple stashes.
git stash list: Shows the stack of stash. Latest on the top.
10. git push: After you have made all the changes and committed its time to push those changes to the remote branch. By using git push, the current branch is pushed to the corresponding upstream branch, but as a safety measure, the push is aborted if the upstream branch does not have the same name as the local one.
This is a very basic version of the actual power of git commands showcased above and you can actually do much more by using different options. We haven't talked about merge, rebase, hooks & much more stuff. I will cover them in future posts and will try to cover each git command in more details.
Since you are still here at this post I am providing few best resource for learning git for beginners as well as seasoned programmers.
1. git init: It initializes the git repository. Running this command creates a directory(.git) inside the current directory which contains git configuration and other repository data. See below image how there was no git repository initially and after running git init, an empty git repository is initialized as we can see the .git directory gets created.
2. git status: It shows the current status of the repository by showing the difference between index file and the current HEAD commit. It shows the untracked files and files which are added to staging area but not yet committed. You can use different options with git status.
Using -s option gives the output in short format.
Option -v or --verbose will show the changes which are staged to be committed.
3. git checkout: It is used for switching between branches.
git checkout <branchName> will switch to the branch. You can also create a new branch and then switch to it in one step by using the option -b.
git checkout -b <branchName>creates a new branch and then switches to the newly created branch.
4. git add: It adds files to the staging area to be committed.
git add <file1> <file2> will add files "file1" and "file2" in the staging area. We can also use regular expressions to add multiple files at once.
git add *.txt will add all files with suffix .txt.
git add . will add all files present in the current working directory.
5. git commit: It commits all the changes which are in the staging area. Running the command git-commit will open the text editor for the commit message to write. Write useful commit message so that even other contributors to the repository should be able to figure out about the changes done in the particular commit.
git commit -m "commit message goes here": By using the option -m, you can directly provide the commit message in the same line without opening a separate editor.
git commit -a : It will add all changes to staging area then commit.
git commit -am "commit message": Adding all files and committing inline.
6. git diff: It shows the changes made since the last commit. To see the changes in the file which are added in the staging area, use git diff --cached.
7. git log: It shows commit logs.
8. git branch: It helps in showing the current working branch.
git branch -d <branchName> can be used to delete the branch.
9. git stash: If you made some change in a branch but want to park those changes for a while and want to work on some different branch, git stash is your friend. It parks the uncommitted changes to a stash area. You can have multiple stashes.
git stash list: Shows the stack of stash. Latest on the top.
10. git push: After you have made all the changes and committed its time to push those changes to the remote branch. By using git push, the current branch is pushed to the corresponding upstream branch, but as a safety measure, the push is aborted if the upstream branch does not have the same name as the local one.
This is a very basic version of the actual power of git commands showcased above and you can actually do much more by using different options. We haven't talked about merge, rebase, hooks & much more stuff. I will cover them in future posts and will try to cover each git command in more details.
Since you are still here at this post I am providing few best resource for learning git for beginners as well as seasoned programmers.
- https://try.github.io/ : This is one of the best resources to start with if you are a novice. It is interactive and easy to start with.
- https://git-scm.com : If you are already using git and want to dive deeper.
Comments
Post a Comment