Learning git was never easy before.

gitCoffee

Got a dream contributing to FOSS projects but lacking confidence in git commands. Don't worry this blog is for you: Let's begin learning git with the following FAQ:

  1. What is git init and why it's used?

    git init: Using this command we first initialize a local git repository. The files we will add here will now be tracked by the git. Running git init command is safe it reinitialize if there is an existing one.

  2. What is git clone and how can I clone a project?

    git clone https://github.com/your_username/repo_name: Will clone the remote repository into a newly-created directory.

  3. What's the status in git?

    git status: This command gives the list of files that have not been committed. This includes modified files and newly created files.

  4. What is Staging? How can I add a file to staging?

    Staging is like a checkpoint where only those files are there which is to be committed. Here we do modify any changes before final commit. git add [file-name.txt]: This command adds the file-name.txt to the staging area.

  5. How can I add all files to the staging area?

    git add -A or git add .: Using this command we can add all new and changed files to the staging area.* Note: There are times when unwanted files like file_name.swp also get added and committed to avoid this it's better to add files one by one.*

  6. How to commit a change with a proper message?

    git commit -m "[commit message]" -m is used to allow adding a commit message
    Using this command we can move all the staging area files to commit area in the local repository.

  7. How can I delete a file or folder?

    git rm file-name or git rm -r folder-name The above command are useful when you are deleting a file or deleting multiple files. git rm -r file-name removes the duplicate file-name recursively.

  8. I have created multiple branches. How can I get all my branches?

    git branch: This command lists all the branches created in your local repository. The asterisk denotes the current branch. git branch -a: This will list all the branches in the local as well as a remote repository.

  9. How to create and delete a branch?

    git branch [branch name]: This command will create a new branch.Note this does not checkout the new branch. git branch -d [branch name]: This command will delete the branch. This is the safest way to delete a branch as it prevents deletion if there are any unmerged changes. git branch -D [branch name]: This command will forcefully delete the branch and also deletes the commits associated with the branch. *Note all the above will bring changes in the local repository.

  10. How can I delete a branch in a remote repository?

    git push origin --delete [branch name]: This command will delete a remote branch.

  11. Can I create a new branch and automatically switch to it?

    Yes, you can use git checkout -b [branch name] command to create a branch and switch to that branch automatically.

  12. How to change branch present in a remote repository?

    Well, It depends on the Git version you are using. For modern version of git: git checkout [remotebranch] For older version of git : git checkout [remotebranch] origin/[remotebranch]

  13. What are the ways to see my commits?

    • git log: Using this we will see all the changes committed in the repositories.

    • git log --summary: Using this we get the detailed view of the changes made in the repositories.

    • git log --oneline: This command gives the history of the commits in brief.

  14. What are the ways to discard changes made to the file?

    • git checkout -- [file-name.txt]: This command will permanently discard all the changes made in the branch local repository.

    • git stash: This command will remove the changes made in the current branch but saves it for later use. So using git stash pop we can get back the saved changes made in the file of that branch.