git for learners (part 1)

Jyothi
6 min readApr 28, 2020

--

https://medium.com/@jyos.sw/git-starters-689fcf86bec1 explains basic git clone, git commit, git push commands and pull request process.

https://medium.com/@jyos.sw/git-for-little-learners-dd48bfba0b61 explains git cherry-pick, git am, git apply, git format-patch commands.

Now we are going to learn some more concepts and commands of git, which can be used in daily git life.

Head

You might have heard ‘Head’ word in git context. So what is Head in the context of git.

Team maintains git repo and it may contain multiple branches. Each branch contains a list of commits. Head points to the latest commit of each branch.

Staged and not staged changes

You might have heard these terms in git context. what are these terms , why used, just a brief:

Suppose there are files named test_v1, test_v2 exist in git repo. In your local cloned directory(or dir), you have modified these two files test_v1 and test_v2 and also you added a new file called test_v3 in your dir.

At this point , if we execute “git status” command it may show like:

Changes not staged for commit:

(use “git add <file>…” to update what will be committed)

(use “git checkout — <file>…” to discard changes in working directory)

modified: test_v1

modified: test_v2

Untracked files:

(use “git add <file>…” to include in what will be committed)

test_v3

This means test_v1 and test_v2 files are not staged to commit these changes. test_v3 file as it is new file and not part of git repo, it shows as untracked changes.

test_v1 and test_v2 are locally modified and not yet staged to commit.

Before doing commit , we need to use “git add” command with files interested to commit.

Here at this point, there are files planned to commit , but not yet committed.

Suppose we are trying to commit test_v1 and executed “git add test_v1” command.

“git status” command may show like:

Changes to be committed:

(use “git reset HEAD <file>…” to unstage)

modified: test_v1

Changes not staged for commit:

(use “git add <file>…” to update what will be committed)

(use “git checkout — <file>…” to discard changes in working directory)

modified: test_v2

Untracked files:

(use “git add <file>…” to include in what will be committed)

test_v3

Here git status command showing staged changes , i.e. test_v1 which is yet to be committed and not staged changes i.e. test_v2 locally modified and if any new files and files exists in your local dir and not known to repo as untracked files.

Staged changes are the changes which are planned to be committed and not staged changes are the changes which exist in your local dir and untracked changes are the files not known to git repo.

tagging

Sometimes there are kind of requirements where we need to capture a point in git repo, for example making customer releases.

There we can make use git tagging where it captures a point or reference. Unlike a branch it does not maintain any history of commits.

git fetch

This command is used to sync your local repo with the remote repo.

git pull

This command is used to sync your development code with the repo you have cloned.

Sometimes if you have your locally modified files and trying to sync with the latest code of repo. It will sync, if there are conflicts in merging, it shows the conflicts which you need to merge manually.

git rebase

This is one of the powerful git command used for many different git operations.

Some operations are to change the commits order, remove a commit, modify a commit message and

integrating changes from one branch to another.

We can do more and more with git rebase command. Usually we will be using git rebase for these operations.

This command can be used in interactive(-i option) approach and non-interactive approach.

I am going to explain an interactive approach for changing commits order, removing a commit and modifying commit message.

Changing commits order:

Suppose if you want to change the commits order in latest 5 commits within the same current branch, you can use the command “git rebase -i Head~5”

It may open an editor and show the latest 5 commits, like:

pick d79f2e4 message 1

pick fa7ddfd message 2

pick 3a8d37c message 3

pick 6d20021 message 4

pick 3b673b9 message 5

# Rebase 347bdcf..b77067f onto 347bdcf (10 commands)

#

# Commands:

# p, pick <commit> = use commit

# r, reword <commit> = use commit, but edit the commit message

# e, edit <commit> = use commit, but stop for amending

# s, squash <commit> = use commit, but meld into previous commit

# f, fixup <commit> = like “squash”, but discard this commit’s log message

# x, exec <command> = run command (the rest of the line) using shell

# b, break = stop here (continue rebase later with ‘git rebase — continue’)

# d, drop <commit> = remove commit

…..

You can change the order of messages and then do save and come out of editor. Git rearranges the commits and returns.

Now you can view the commit order by using the commands “git rebase -i Head~5” or git log or git log — oneline.

If you are using git rebase -i Head~5 command , if you do not want any changes , you can directly come out of the editor without doing any changes.

Removing an existing commit:

Suppose that you have mistakenly committed some files or some problems with the commit you made.

You can revert back that commit using git rebase.

Suppose that commit is existing in latest of 5 commits (same above example), you can use the interactive mode git command “git rebase -i ~Head~5”

If you take the same above example and want to remove commit corresponding to “message 3”, you edit the log by modifying “pick” to “drop” that corresponding line:

pick d79f2e4 message 1

pick fa7ddfd message 2

drop 6d20021 message 3

pick 6d20021 message 4

pick 3b673b9 message 5

# Rebase 347bdcf..b77067f onto 347bdcf (10 commands)

………

Save changes, come out of the editor.

Suppose if there are conflicts or some dependencies of the commits above commit of “message 3”, git not able to remove it successfully,

it may show message like:

Auto-merging XXXX_FILES

CONFLICT (content): Merge conflict in XXXX_FILES

error: could not apply bd337f8… message X

Resolve all conflicts manually, mark them as resolved with

“git add/rm <conflicted_files>”, then run “git rebase — continue”.

You can instead skip this commit: run “git rebase — skip”.

To abort and get back to the state before “git rebase”, run “git rebase — abort”.

Could not apply bd337f8… message X

You can either abort using “git rebase — abort” command or continue with the following steps:

· Resolve those conflicts ,

· Use “git add/rm” commands to commit those modifications,

· Use command “git rebase — continue”

and after successful git rebase you can push those changes your git repo.

Modifying commit message

Suppose if you want to modify the commit message, you can do by using “git rebase -i Head~n” , n is number where you would want to view the latest n commits.

If we take the same above example and want to change the “message 4” corresponding commit, you can use the git command “git rebase -i Head~5”, change the word “pick” to “reword” , save and come out of editor:

pick d79f2e4 message 1

pick fa7ddfd message 2

pick 3a8d37c message 3

reword 6d20021 message 4

pick 3b673b9 message 5

# Rebase 347bdcf..b77067f onto 347bdcf (10 commands)

………

Next git shows the corresponding commit message, where you can modify and save, and after successful git rebase you can push those changes your git repo.

Integrating or merging your changes onto latest git repo

Suppose you are developing a feature , you branched out from master branch and started developing ,after some days some bugs got fixed on that master branch and you got to take those changes into your development code.

There are multiple methods, one is to use git rebase command.

You are in your current branch dev_feature1 and you want to get the latest commits of master branch and apply your changes on top of master branch.

Use “ git rebase -i master dev_feature1” or “git rebase -i master” or “git rebase master” commands

If there are no conflicts, it will directly apply your commits on top of master branch. If there are conflicts , you can resolve, commit and continue rebasing.

Note that in “git rebase <base> “ command, base can be a commit id or Head or Head~n or tag or branch name . When <base> is commit id or Head or Head~n , then changes are local to that current branch.

--

--

No responses yet