Github Tips and Tricks

GitHub is a code hosting platform for version control and collaboration. It lets you and others work together on projects from anywhere.

Popular Github Commands

git clone [URL](Clone: Obtain a repository from an existing URL:)

git init (Initialize a Git repository)

git add . (Add changes)

git commit -m "Your message" (Commit changes with a custom message.)

git push -u origin [BRANCH_NAME] (Push changes to the branch eg. [BRANCH_NAME] and set it to track the remote.)

git checkout PARENT_BRANCH

git pull ( pull latest changes from current remote branch)

git checkout -b [BRANCH_NAME] (Create and switch to a new branch)

git merge origin/PARENT_BRANCH (Merge the PARENT_BRANCH branch remote into the current branch.)

git commit then :wq!(save and quit) and then push

git reset --soft HEAD~1 (Undo last commit)

git restore -staged [FILE_NAME] (To upstage or even discard uncommitted local changes)

git clean -fd (Remove untracked files and directories.)

git stash ( Save your uncommitted changes and revert to the last commit. )

git log (View the commit history of the current branch.)

git log –first-parent (check the parent branch)

git status (Check the status of the repository (modified, untracked files, etc.).)

git diff (Shows the differences between the working directory and the staged files.)

git checkout -b [branch_off_to_branch] [branch_off_main_branch] (branch off eg. git checkout -b [BRANCH_NAME1] [PARENT_BRANCH])

git branch -d [BRANCH_NAME] (Delete a branch locally)

git push origin –delete [BRANCH_NAME] (Delete branch remotely: )

git rm -r [FILE_NAME] (Delete file)

git branch -m [old_branch_name] [new_branch_name] (Rename the local branch to new name)

git push origin [old_branch_name] [new_branch_name] (Delete the old-name remote branch and push the new-name local branch)

Issues Solutions:

#Issue 1: If pull is not possible

git reset

git rm -r –cached

#Issue 2: Revert

git revert HEAD~1 (1 commit, last commit: the target commit is 1 commit behind the head commit on the currently checked-out branch)

git revert HEAD~2 (2 commits, till second last commit: the target commit is 2 commit behind the head commit on the currently checked out branch)

git revert [COMMIT_ID]

# Issue 3: Reset (Removed last commit from current branch)

git reset HEAD~1 –soft (for local)

git push origin [BRANCH_NAME] (for remote changes)

#Issue 4: File aborting so cleaning is important

git clean -n

git clean -f

git clean -fd

git clean -d -f

git stash

#Issue 5: Git cache removed

git rm -r –cached

Popular command for terminal

rm -r [FILE_NAME] (Delete file)

touch [FILE_NAME] (create file)

mkdir [DIRECTORY] (To create a new directory)

ls

ls -la