Git & SVN

Summary: Understanding some advanced Git commands, Git Stash, Git Reset, Git Bisect, Git Rebase, Git Restore and Git Amend also learn about basic commands of SVN.

Git

Git is a distributed version control system designed to handle everything from small to very large projects with speed and efficiency. It was created by Linus Torvalds in 2005 for the development of the Linux kernel. 

Git Stash

Git stash enables you to save your code without making a commit. This command is helpful when you’ve already made three neat and tidy commits, but you also have some uncommitted code that’s quite messy; you won’t want to commit it without removing your debugging code first. Then, for some reason, you suddenly need to attend to another task and have to switch branches.

When you run git stash, the uncommitted code disappears without being committed. Stashing is like saving a temporary local commit to your branch. It is not possible to push a stash to a remote repository, so a stash is just for your own personal use.

$ git stash
Saved working directory and index state WIP on my-feature: 49ee696 Message

List all the Git Stash with:

$ git stash list
stash@{0}: WIP on my-feature: 49ee696 Message

To restore the Git Stash use:

$ git stash apply stash@{0}

Git Reset

If you do find yourself in the situation where you’ve accidentally committed some messy code, you can do a “soft” reset. This means that the code appears as if it has not been committed yet. Then you can tidy up your code in your IDE before making a cleaner commit. To do this you can run git reset --soft HEAD~1. This will reset the most recent commit. You can reset back more than one commit by changing the number after ~ e.g. git reset --soft HEAD~2.

$ git reset --soft HEAD~1

Git Bisect

Git bisect is used for a singular purpose, to locate a specific commit that introduced a change or bug into your code. It does this by using a binary search algorithm to help you identify the bad commit. This means that git bisect will help you efficiently search through your commits to find the problem, by repeatedly cutting the number of potential problematic commits in half.

$ git bisect start
$ git bisect bad commit-hash
$ git bisect good commit-hash

Git Rebase

Git rebase is a powerful feature of Git that has the ability to rewrite and reorganize your commit history. Git rebase allows you to change the base of your branch.

Unlike merging, which creates a new merge commit and combines the history of both branches, rebasing replays the commits of one branch onto another. This results in a linear commit history, making it easier to read and understand.

0sbSYMp0Xhbod-lkHDr-24wrB4NWov857B7C_vnpkQlM7bTq1qFbGMcWqfYrDHWDs1lHlxm9ZyLJaRfRr0pmcqVf7nfe_siDLv0dWdq2ZtoCUboChjR_9SzAAtMOVapYaK3kF40E3CY_qrbEveA6_jk
  1. Switch to your main branch with git checkout main
  2. Update your local main branch with git pull. This is so we have the latest HEAD of main available for the rebase.
  3. Switch back to your branch foo with git checkout foo
  4. Use git rebase main, this will complete the rebase, replaying your commits on top of the HEAD of main.

ref: https://www.freecodecamp.org/news/how-to-use-git-rebase/

Git Restore

With Git, you can easily restore a file to its default state using a fairly new command: git restore. In fact, you should consider this a replacement for git reset in most circumstances, as it offers a lot more power under the hood. For instance, you can achieve the same outcome with git restore --staged <filename> as you can with git reset HEAD.

$ git restore <filename>

This will remove the changes from the working directory as if nothing had happened. As with git checkout -- <filename>, you want to ensure you don’t want any of the local changes, as they will be gone forever.

Git Amend

There will likely be lots of times when you push a commit and then realize you forgot to include something important. With Git, you can easily amend the commit to include the missing changes.

$ git add .
$ git commit --amend
$ git commit --amend -m "New commit message"

ref: https://kinsta.com/blog/advanced-git/

SVN

SVN is a simple and open-source license Software Configuration Management (SCM) tool that can be used in all the phases of Software Projects.

  • SVN is a Subversion control tool that helps us to maintain all the project artifacts in a script repository.
  • It’s a free/open-source tool that helps us to manage software versioning and revision control system.
  • It is easy to understand and works faster when compared to the other tools (For Example, GIT, mercurial).

Repository: It is a central place or repository where all our project artifacts like (Design, Source code, Documentation, test cases) are stored. Individual users can locally check out the files in their local machine and can work on it.

As it maintains all the history information of the particular artifact, the users can go back and look into the log to see “Who & When & Why” has changed.

SVN Checkout: It is a process of taking the project artifacts from the central repository to the local machine. Users can do modifications and can save changes locally.

Commit: It is a process of saving the changes from local machines to the central repository. During the commit, we should provide meaningful commit messages so that the other users can easily understand.

SVN Workflow

svn admincreate

The svn admincreate command creates a new, empty repository. 

svn import

The svn import command commits an unversioned tree of files into a repository (and creates intermediate directories, if needed). 

svn checkout

The svn checkout command checks out a working copy from the repository. This command is sometimes shortened to svn co.

svn commit

The svn commit command sends your changes back to the SVN server.

svn add

The svn add command will add a new file to the repository — but only after you’ve done a svn commit.

svn delete

The svn delete command will delete a file from your working copy of the repository. 

svn list

The svn list command allows you to see a list of files in a repository without creating a working copy. 

svn diff

The svn diff command reveals the differences between your working copy and the copy in the master SVN repository.

svn status

The svn status command prints the status of working copy files and directories.

svn info

The svn info command displays information about a local or remote item.

svn log

The svn log command shows log messages from the repository.

svn move

The svn move command moves a file from one directory to another (or renames it). 

svn merge

The svn merge command combines two different versions into your working copy.

svn revert (i.e., undo svn add)

The svn revert command reverts changes in your working copy, as well as property changes. For example, you can use svn revert to undo svn add.

svn update

The svn update command updates your working copy with changes from the repository.

svn shelve

The svn shelve command stores your changes without submitting them.

ref: https://www.perforce.com/blog/vcs/svn-commands-cheat-sheet

«
»

Leave a Reply

Your email address will not be published. Required fields are marked *