Git Useful commands

Github project creation: 

Steps:  

echo "# audio" >> README.md

git init
git add README.md
git commit -m "first commit"
git branch -M main
git remote add origin https://github.com/yourGitRepositoryLink
git push -u origin main

 Update password 

git remote set-url origin https://{new url with username replaced}


git remote set-url origin https://YOURUSERNAME:YOURPASSWORD@1.1.1.1.1/yourteam/appname.git



Git commands list:

  • #1 -git create branch 

mac-YourName:~ username$ git checkout -b feature-branch master 

  • #2 -git force pull 

mac-YourName:~ username$ git pull 


# 2.1 - overwrite local with git pull 

    mac-YourName:~ username$ git stash

      mac-YourName:~ username$ git pull 

            • #3 - git remove untracked files 
            do a dry-run to see what will be deleted 

            mac-YourName:~ username$ git clean -n -d  


            after, do git clean to delete it 

            ac-YourName:~ username$ git clean -f -d  

              • #4 - git unstage
                      adding files to the working tree means you are adding them to the staging area, means you are staging them. 
                  mac-YourName:~ username$ git add 
                    If you want git to stop tracking specific files on the working tree, you need to remove them from your stage files... 

                mac-YourName:~ username$ git rm -cached <file-name> 

                to leave the entire working tree untouched, unstage all files. 

                mac-YourName:~ username$ git reset  

                    • #5 - git undo merge 
                            Checkout master branch : $git checkout master 
                            Get list of all merge commit : $git log --oneline 
                            Revert merge by commit : $git revert -m 1 <merge-commit-id>
                            Commit the revert and push changes to the remote repo : $git commit -m "revert merge #123 "
                        • #6 - git remove files on remote 
                                Remove files: $git rm <file-1> <file-2>
                                Commit your changes : $git commit -m "removing files"
                                Push changes : $git push 
                            • #7 - git uncommit 

                              keep changes from commit you want to undo: $git reset --soft HEAD^

                              destroy changes from commit you want to undo: $git reset --hard HEAD^


                              • #8 - git diff between branches 
                                      $git diff branch_1..branch_2 
                                      produce difference : $git diff master...branch_test 
                                      compare difference : $git diff branch1:file branch2:file  
                                  • #9 - git delete tag 

                                    delete tag from remote is origin: $git push origin :refs/tags/<tag-name> 

                                      delete tag locally: $git tag -d <tag-name> 


                                      • #10 - git rename branch 
                                              checkout branch to rename: $git checkout <old-name>
                                              rename branch locally: $git branch -m <new-name>
                                              delete old branch from remote: $git push origin :<old-name> <new-name>
                                              reset the upstream branch for the new branch name: $git push origin -u <new-name> 

                                            Comments

                                            Popular Posts