Few Essential GIT Commands

Clone
For cloning the code run the below command
$ git clone <url of the forked code> <optional folder name>
e.g. $ git clone https://github.com/xxxxxxx/yyyyyyy.git <folder name your want to give>



Note: If we are cloning then there is no need to run the command “$ git init” to create .git subdirectory containing the necessary git repository files. It will be created automatically.

Commit
Commit the changed files
$ git commit –a –m “<message>”


Un-Commit
Un-Commit the last commit but KEEP the changes
$ git reset --soft HEAD~
Un-Commit the last n commits but KEEP the changes
$ git reset --soft HEAD~n
Un-Commit the last n commits and DISCARD the changes
$ git reset --hard HEAD~n


Recover Deleted Commit
Get the commit id you want to recover
$ git reflog 
Merge the lost commit
$ git merge <commit id>


Amend Last Commit
Modify the last commit
$ git commit --amend –m “<message>”

Log
List all the commits on the current branch
$ git log
List all the commits on a given branch
$ git log <branch name>

Show
Show the details of changes in a commit on the current branch
$ git show <commit id>
Show the details of changes in a commit on a given branch
$ git show <branch name> <commit id>

Add (Staging files)
Tracking new files or Staging modified files
$ git add <file name>

Revert Changes
Revert changes in a single file
$ git checkout -- <relative path of file>
e.g. $ git checkout -- EmployeePortal.Web/EmployeePortal.Web.csproj
Revert all the changes
a.      Unstage staged file (staged using git add)
$ git reset
b.      Revert all local uncommitted changes (should be executed in repo root)
$ git checkout
c.       Remove all local untracked files, so only git tracked files remain
$ git clean -fdx

Pull
You can pull the latest code using the below command when at master
$ git pull upstream
After you pull run the below command
$ git merge upstream/master
You can open the merge tool for merging using the below command
$ git mergetool

Rebase
Rebasing master with your current branch
Go to the current branch and then run the below command
$ git rebase master

After rebasing is complete, you run the below command
$ git rebase –continue

To abort rebasing run the below command
$ git rebase –abort

Squash
For squashing the commits run the below command
$ git rebase –i head~<count of commits to squash>
a.      Press “Insert” key
b.      Then write pick for the first commit and squash all the other commits
c.       Press escape
d.      Press “Shift” and “;”
e.      Write “wq”
f.        Choose from amongst the comments given for all the commits. You can also modify the comments and remove some of them.
g.      Press escape
h.      Press “Shift” and “;”
i.        Write “wq”

Push
Execute the below command from the branch you want to push (the option f shows that you want to do a forceful push)
$ git push origin <branch name> -f

Remote
Setting the “upstream” or adding an alias

Note: Upstreams are used for fetching the latest code whereas the origin is used for pushing the changes

$ git remote add <name of upstream> <git url for the master>
e.g. $ git remote add upstream git@gitproxy:xxxxxxx-yyyy/zzzzz.git
Listing remote aliases
$ git remote
Listing remote aliases with urls
$ git remote –v
Remove an existing remote alias
$ git remote rm <alias name>
e.g. $ git remote rm upstream
Renaming an existing remote
$git remote rename “<initial>” “<final>”

Clean
For cleaning the local branch run the below code. This will remove all the bin, debug etc folders.
$ git clean –xfd

General
Fetch the latest code from upstream
$ git fetch <upstream name>
Status of files in the working directory and branch
$ git status
Change branch name


$ git branch –m <oldname> <newname>

Comments

Popular Posts