Git has lot of command line options. Since isn’t harder to refer huge manual each time. By keeping that in mind, gathered handful list of Useful git commands. Organized this document to match with development lifecycle. Such as a developer start configure repository, develop and submit code.
For demonstration, here using Linux system installed with git package.
Level 1 – Useful git commands
This section lists out required and basic commands across different stage of development life cycle.
Key Terminology
- Remote – Refers to a repository stored in remote server
- Clone – Creates local copy of repository from remote server
- Stage – Like a workspace. Working area of local repository. Changes are stored in stage until commit
- Commit – Save changes to repository
Configure Git Repository
- Clone repository
$ git clone <repository url>
For example, HTTPS – Access git repository over HTTPS protocol
$ git clone https://github.com/adamdev/Ubuntu-OS-upgrade.git
SSH – Access git repository over SSH protocol
$ git clone git@github.com:tmsundaram/Ubuntu-OS-upgrade.git
- Configure git user profile
$ git config --global user.email adam.developer@example.com
$ git config --global user.name adamdev
To view the global git profile configured (Linux command)
$ cat ~/.gitconfig
- Know repository configuration files
Repository specific configurations are stored within repository folder with name “.git“. Note it is a hidden folder.
- Authentication
The write operation to the remote git repository requires user authentication. For instance push, pull and fetch operations enforces authentication with git server.
- Use username and password combinations for HTTPS repository URL
- SSH key pair combination used for SSH protocol. Refer Github SSH setup guide for detailed Information about SSH key configuration.
Use the following command to associate SSH Key with current git repository configuration. Its good practice to set passphrase for the private key.
$ git config --global core.sshCommand "ssh -i /root/.ssh/id_rsa -o StrictHostKeyChecking=no"
Development Phase
In Useful git commands series up next is development time reference guide.
What is Branch? – Like a multiple development streams. Thus, take snapshot of specific state of code and start development from there. Hence no need to bother even if underlying base code changes. Then review and approve code changes before merge into master repository.
Commands listed in this section are operate against local copy of repository. But changes gets updated to server during push action.
- Create branch locally
$ git checkout -b <branch-name>
For example:
$ git checkout -b "sun-20200509"
- Save code changes locally
$ git commit -a -m "fixed bug#0001"
// -a => save all changes
// -m => comments for this changes
During development, commit (save locally) as many times as needed. For example,
$ git commit -a -m "fixed bug#0002"
$ git commit -a -m "fixed bug#0003"
- Review git status
Shows current branch and synchronization status against remote repository
$ git status
--Output--
On branch master
Your branch is up to date with 'origin/master'.
nothing to commit, working tree clean
End of development
Push changes to remote git server
$ git push origin <branch-name>
//origin - Alternate default name to remote repository. Standard convention to make things easier.
For example,
$ git push origin sun-20200509
Now the code changes are in remote git server. But not yet in actual master repository. To do that Initiate a “pull request”.
Note: Pull request mentioned here different from pull action in git command. And for reference details of pull CLI action detailed in next section.
Pull request – Kind of code approval flow. For example, developer request to merge “sun-2020509” with master branch through approval process. Possible actions with pull request,
- Initiate pull request from github web UI. Because its not possible over CLI.
- Put description about development and assign request to reviewer
- Collaborate between reviewer and developer such as comment, followup, approve, reject, re-open and change code
- Once approved merge developer branch into master
Level 2 – Useful git commands
Lets step up further in this section. Here brought in additional concepts in git along with reference documents. But still these are also much Useful git commands.
Remote operations
Get URL configured to access the remote repository
$ git remote -v
--sample output--
origin https://github.com/adamdev/Ubuntu-OS-upgrade.git (fetch)
origin https://github.com/adamdev/Ubuntu-OS-upgrade.git (push)
Set URL – Change the repository URL in current configuration. For instance this command is to change URL from HTTPS to SSH.
$ git remote set-url origin git@github.com:adamdev/Ubuntu-OS-upgrade.git
Pull – To get latest code changes from remote git server. In other words, synchronize from remote repository branch to local branch. Following commands download the code changes from remote repository to local repository. And if no branch specified by default it chooses master branch.
//Download code for master branch
$ git pull origin
//Download code for specific branch
$ git pull origin sun-20200509-04
Push – Quite opposite to pull command. It upload changes from local repository to remote repository. Following commands is an examples to upload changes for master and specific branches.
//Upload code for master branch
$ git push origin
//Upload code for specific branch
$ git push origin sun-20200509-04
Actions on Branch
List branch available within local repository
$ git branch --list
--Output--
master
* sun-20210616
// * - Indicate current branch in use
Delete branch from local repository
$ git branch --delete sun-20200509-04
Switch branch within current repository
$ git checkout sun-20200509-04
--Output--
Switched to branch 'sun-20200509-04'
Current branch – To view current branch in use,
$ git status
--Output--
On branch sun-20200509-04
Your branch is up to date with 'origin/master'.
nothing to commit, working tree clean
// Refer to first line "On branch"
Repository file management
File permissions are carry forwarded from remote git repository during clone. Since its recommended to maintain correct permissions for all the files within repository. As specially for new files, owner must ensure to assign right permissions. The following commands help to view and modify permissions within git.
ls-files – View file permissions – Git uses Linux file permission standards. See the document Understanding Linux file permissions to know more on this topic.
$ git ls-files --stage
--Output--
100755 856ba3d629c56de082d4155409eedeb9068a79ff 0 file_executable.sh
100755 f48b0ca92144ffdf18a2a142c295fa8f547dcca7 0 js_bin_file
100644 94384e1dbf25f687888c834b62bc08d6e7680d0e 0 data.db
From above sample output,
//100755 - file read and executable by everybody
//100644 - Others can only read the file but owner can read & write
Update-Index – Modify file permissions within git,
For instance to set execute permission for a file,
$ git update-index --chmod +x file_executable.sh
Similarly, this one removes execute permission from a file
$ git update-index --chmod -x data.db
Revoke / Undo Changes
Lets assume, some changes made to the code locally but not executed commit yet. And for some reason, need to undo changes made to local stage area. Following commands are useful In such scenarios.
Hard reset – Do hard reset and Ignore all the local changes. This command revert the stage area back to last commit.
$ git reset --hard
Reset to previous commits – Should specify head position along with reset command to undo changes of any previous commits.
$ git log --oneline
a2c05f5 (HEAD -> sun-20200509-04) testing stash function
08a5e44 (origin/master, origin/HEAD, master) Update README.md
0be4c82 Create README.md
b5dda67 first commit. Test completed
$ git reset --soft HEAD~1
$ git log --oneline
08a5e44 (HEAD -> sun-20200509-04, origin/master, origin/HEAD, master) Update README.md
0be4c82 Create README.md
b5dda67 first commit. Test completed
//HEAD~1 => Resets local repository back to previous to last (n-1) commit
//HEAD~3 => This will reset repository previous to last 3 (n-3) commit
Refer to the article Undo last Git commit for more details on this topic.
Undo and Redo Changes
Undo local changes (Stash) – Its similar to hard reset except it saves changes for later recovery. Following command save the local changes as stash. Then revert the stage area back to latest code available in server.
$ git stash save
--Output--
Saved working directory and index state WIP on sun-20200509-04: a2c05f5 testing stash function
List stash records for a current repository
$ git stash list
stash@{0}: WIP on sun-20210616: a2c05f5 testing stash function
stash@{1}: WIP on sun-20210616: a2c05f5 testing stash function
Redo or Reapply local changes and remove from stash. Following command reapply previous code changes from stash to local repository. And then removes from stash.
$ git stash pop stash@{0}
$ git stash list
stash@{0}: WIP on sun-20210616: a2c05f5 testing stash function
//stash@{0} => Zero (0) refers to stash Index number.
//Above example of redoing the change and remove it from stash
Redo and keep changes in stash. Following command reapply previous code changes to local repository. And also keep them in stash as well.
$ git stash apply stash@{1}
$ git stash list
stash@{0}: WIP on sun-20210616: a2c05f5 testing stash function
stash@{1}: WIP on sun-20210616: a2c05f5 testing stash function
//stash@{0} => Zero (0) refers to stash Index number.
//Above demonstrates redo and keep changes in stash too
To have in-depth knowledge on stash concept refer to article git-stash
Hope above git commands collection is handy to use. If there is a feedback or query, feel free to post it on comments section.