30 Must Know Git Commands

  1. git init
    1. Used to create a new repository
    2. Used for setting up your professional project.
    3. Executing this command creates a local repository with a default main branch.
  2. git remote add origin https://github.com/your-username/your-repository
    1. Used to connect this local repository to github.
    2. Connect remote repository's origin with the local repository
  3. git push -u Remote-Name Branch-Name
    1. Push the main branch on github
  4. git checkout -b New-Branch-Name
    1. Make a new Branch
    2. Used to create a new branch and put all the code in it
    3. This keeps the existing code safe from bad implementations
    4. The checkout command creates a new branch if it does not exists in the repository.
    5. The "-b" option switches or checks out from the current branch to newly created branch.
    6. It is always better to switch to the main branch before creating new branches as it has the most up to date code.
    7. Once a new branch is created it should be pushed to the remote repository.
    8. The checkout command works well if we have no uncommitted updated files else it is better to stash them or commit them in current branch before switching to new branch 
  5. git switch Branch-Name
    1. used to switch to an existing branch in repository
    2. one must pull the latest code from github for this branch so the local branch is up to date with remote branch.
  6. git branch
    1. Gives you information about branches in the repository
  7. git stash
    1. stash/un-stash files
    2. One of the ways to resolve checkout error
    3. A temporary save is made of work done in current branch - if we are not ready to commit changes in current branch.
    4. For example changed db/configuration files in local etc.
  8. git stash pop
    1. To undo and recover the stashed changes.
    2. We can come back to the same branch and pop them.
    3. We can use it in another branch too but it may cause conflicts which we must resolve.
  9. git stash list
    1. Each time we stash code it is saved in a stash stack to view the stashed entries in the stack we use git stash list.
  10. git stash apply stash@{index}
    1. If we do not want to apply stash at the top of the stack instead want to apply from one within the stack we use this.
    2. Does not pops stash from stash stack
  11. git status
    1. Check Branch Status
    2. Gives us the status of current branch.
    3. We get all the staged or untracked changes.
  12. git branch -m Old-Branch-Name New-Branch-Name
    1. Rename Local Branch
    2. This is used to make a consistent git directory structure,
    3. One of the handy commands when there is a typo while creating a branch.
  13. git push origin: old-branch-name new-branch-name
    1. Rename remote branch
    2. Once you have renamed the local branch it is time to change the name of the corresponding remote branch.
    3. This command deletes the branch with the old name and creates a new branch with same code base.
  14. git add .
  15. git add File-Path
    1. Synchronize branch changes.
    2. once a new file has been created or an existing file has been updated we need to add those files to our next commit.
  16. git commit -m "Commit-Message"
    1. When all the changes have been added to the commit, it's time to commit these changes and write a unique commit message.
    2. Commit message can be any String that can describe your work.
  17. git push
    1. To push this commit on remote repository we use git push.
  18. git commit --amend -m "Changed Commit Message"
    1. Used to update or amend the previous commit.
    2. we can also add new files before amending.
    3. If some issue is raised during the code review use the "git amend" instead of creating a new commit to fix the issue.
  19. git clone Repositoty-Url
    1. clone repository
    2. This command is the first command to clone/checkout the project.
    3. Once a project is cloned you must create your own branch,add new changes and merge the code back to master branch or branch dedicated to specific environment.
  20. git log
    1. Checking commit log
    2. A log is used to keep track of commits in a branch
  21. git reset-hard origin/Branch-Name
    1. Reset to last commit
  22. git fetch Remote-Name , git merge Remote-Name/Branch-Name
    1. Merge local repository with remote repository
    2. We maintain 3 copies of a code
      1. local copy where tasks in progress are maintained.
      2. staging copy where the tasks deployed are shared with the client.These tasks are reworked upon based on the feedback received from the client and deployed back to staging server.
      3. once client approves the changes they are deployed on production server.
    3. Every time a new feature is deployed we perform a merge operation.
    4. Updated code files from github remote repository are merged with the local repository on these 3 servers.
    5. Most common issue is merge conflict.
    6. These conflicts must be resolved to complete merge operation.
    7. The Merge operation is performed in 2 steps
      1. We fetch or download the remote code base from github.
      2. Then merge the remote and local branch histories.
  23. git pull remote-name branch-name
    1. Another way to perform merge is git pull. Pulling works in the same way as merge with the added benefit of fetching.
    2. Git pull performs both fetch and merge joining two or more branches.
  24. git cherry-pick Commit-Hash
    1. Move a commit from 1 branch to another
    2. Used to move a code between branches for different environments.
    3. This command applies selected commit on the current branch, which is also quite handy during bug hot fixes.
    4. Cherry Picking may cause duplicate commits so merge is the most preferred way in many scenarios.
  25. git clean -f , git clean -fd 
    1. Remove untracked files and directories
    2. Files and directories which have not been committed yet can be easily removed from the working directory.
  26. git branch -d BranchName
    1. Delete a branch on local repository
    2. If a branch is no longer required it is always better to clean repository by deleting that branch.
  27. git branch -D Branch-Name
    1. To forcefully delete a local branch
  28. git push Remote-Name --delete Branch-Name
    1. Delete a branch on remote repository
    2. Delete a branch on the remote repository is similar to push an update on remote using the same git push command with --delete option
  29. git config core . fileMode false
    1. Ignore git permission changes
    2. While working on Linux based system we may need to change file permission to 777 to make them executable. Git picks up these permission changes and shows them as updated files.
    3. We can ignore these changes by setting the file mode flag to false.
  30. add .gitignore;touch .gitignore; git rm -r -- cached; git add .; git commit -m "git ignore fixed untracked files"
    1. Fix .gitignore
    2. .gitignore file helps us to ignore committing unwanted files to the repository.
    3. files such as your local configuration changes local db changes need not be tracked by Git

Comments