Monday, 15 January 2024

Useful git commands

 Introduction:

Git is widely used versioning control system with millions of lines of code maintained through it world wide. To maintain file versioning git is integrated with other systems like Github, bitbucket and others. 


Commands:

Some basic and useful commands of git include the following:

1- git status

  • To get the current status of user branch use this command git status
  • This command will tell if there are any changes in any files that need to be stage and commit
  • Staged files are files that are ready to be committed to the 
  • If there are any deleted files
  • If there are any new files added
2- git log 
  • Use this command to check the history of changes (commits) made in the current branch
  • It helps in tracking the previous work
  • Also see git log origin/<master>
  • To check logs from a specific user via its name use: git log --author="NAME"
  • To check logs from a specific user via its email use: git log --author=<email> 
3- git help
  • This command provides information related to different git commands
4- git branch
  • This command lists down all the branches that are present in locally.
  • To view only the origin (remote) branches use: git branch -r
  • To view all the branches local and remote use: git branch -a
  • To delete a local git branch use: git branch --delete <branch>
5- git branch <new-branch>
  • Use this command to create new branch locally
6- git checkout
  • Use this command to move from current branch to selected branch
  • git checkout -b <new-branch>. This will first create new branch if not exist then move to it from existing.
  • git checkout -b <new-branch> origin/<existing-branch>. This will checkout new branch from an existing remote branch.
  • git checkout <commit-id>. To checkout a specific commit.
7- git add
  • git add index.html. This command will stage file for commit.
  • git add -A or git add --all. This command will add all files to staging.
8- git commit
  • Each commit is supposed to be a change point or save point by git
  • git commit -m "<message>". This command will commit changes after staging
  • git commit -a -m "<message>". This command will commit changes without staging
9- git fetch
  • This command will fetch latest file versions from all remote branches
10- git push
  • git push origin "<branch>". This command will push local branch to remote new branch.
  • git push origin. This will push code to remote same branch.
  • git push --set-upstream origin <branch>. This command sets upstream branch for pushing code to remote repository on first time.
11- git pull
  • This command will get most recent changes of  remote branch to local copy
  • git pull origin
  • git pull origin <branch>.This will fetch a specific branch from remote and merge into the current branch.
12- git clone
  • Creates a full copy of repository in local directory
13- git revert 
  • This will take previous commit and add it as new commit
14- git reset 
  • This will move repository back to a previous commit
  • git reset <commitId>, reset to a specific commit
  • git reset --hard
  • git clean -fxd
15- git stash
  • Stashes(saves and sets aside) the work in local directory, this is typically helpful in scenario where one is not done with current work but suddenly has do something else then the current work can be stashed and later on re-applied once the second work is done.
  • git stash list. List down the stashes.
  • git stash show. Inspect the stashed items.
  • git stash apply. Restore stashed work.
16- git remote
  • git remote add origin <URL>. This command specifies a remote repository as an origin to the local Git repository.
  • git remote show origin. This command tells which local branches are tracking which remote branches.
17- git merge
  • This command is for merging the work of two branches.
  • The way to do a merge is that first checkout the branch for e.g master branch with:
  • git checkout master
  • Now merge with other branch with following command:
  • git merge <new-branch>
  • Now delete the branch with: git branch -d <new-branch>

I hope this will help you as reference while using git. Thank you and keep on reading.

No comments:

Post a Comment